0

I have a class in PHP. In the constructor I define a value for a property and then I have to access it in a method. However, I get keep getting Fatal error: Using $this when not in object context. I need to access the property from within the class.

class Base {
    public $vat;
    public function __construct() {
        $settingsClass = new GeneralSettings();
        $generalSettings = $settingsClass->getSettings();
        $this->vat = $generalSettings['vat'];
    }
    public function vatPrice($price) {
        $vatPrice = $price + (($this->vat / 100) * $price);
        return self::formatPrice($vatPrice);
    }
}
schmitsz
  • 175
  • 1
  • 13

1 Answers1

0

I've tested your class, with a simple value, and I found no errors.

class Base {
    public $vat;
    public function __construct() {
        $this->vat = 75;
    }
    public function vatPrice($price) {
        $vatPrice = $price + (($this->vat / 100) * $price);
        return self::formatPrice($vatPrice);
    }

    public static function formatPrice($price) {
        echo $price;
    }
}

$myClass = new Base();
$myClass->vatPrice(100);

Note that formatPrice is a static function.


  1. Sometimes you want to refer to some attribute of one instance of the class, only for one object, so in this case you must define an attribute or method of the form visibility $bar; for a variable or visibility function bar($args...) and you can access with $this, because $this is the reference of the actual instance of the class (the current object.)
  2. Maybe you want to get the same value in some attribute for all instances of that class, ie: a static attribute/method. In this case, you have to define visibility static $bar for a variable or visibility function $bar($args...) for a function and you can access them with the self keyword.

visibility is public, protected or private

When you have something like:

class Foo {
    ...
    public static function bar() { ... }
    ...
}

The bar() function is invoked as follows:

  • Outside of Foo: self::bar();
  • Inside of Foo: Foo::bar();

Conversely, if you have something like this:

class Foo {
    ...
    public function bar () { ... }
    ...
}

Then,

  • Outside of Foo:

you must first instantiate the class, and from that object access the bar():

$myObject = new Foo();
$myObject->bar();
  • Inside of Foo: $this->bar();

Please, see the static keyword reference in the PHP documentation.

Tomás Juárez
  • 1,517
  • 3
  • 21
  • 51
  • 3
    This could be a part of a OOP php tutorial, but i don't see a answer for OP's question – Rizier123 Feb 04 '15 at 00:10
  • I'm trying to help fix the error, since he did not have any specific question. – Tomás Juárez Feb 04 '15 at 00:18
  • 1
    @Rizier123 haven't Tomas explained how the method must be invoked? SO is not a "fix my code for free" community. And this answer clearly explains what to do to avoid mistake that OP has made. – zerkms Feb 04 '15 at 00:30
  • @Rizier123 I have run the code and I haven't found any error, then all I can say is that this error occurs when you do not respect what I put in the answer. – Tomás Juárez Feb 04 '15 at 00:33