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.
- 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.)
- 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,
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.