class A{
<code> public $age = 50; private $money = 2000; static public $head = 1; public function tell(){ echo $this->age,'<br />'; echo self::$head,'<br />'; } static public function sayMoney(){ echo $this->money,'<br />'; }</code>
}
class B extends A{
<code> public $age = 22; private $money = 10; public function subtell(){ parent::tell(); echo $this->age,'<br />'; } public function subMoney() { parent::sayMoney(); echo $this->money,'<br />'; }</code>
}
$b = new B();
$b->subtell();//22 1 22;
echo '
The last sentence reports an error Using $this when not in object context
But when subMoney() is called, $this is not bound, $this points to the b object, and then executed parent::sayMoney(); is not bound to $this because it is called statically. Shouldn't it get 2000 when sayMoney() is executed? Why does it report an error? Is it different from the previous $b->subtell(); call? Same
class A{
<code> public $age = 50; private $money = 2000; static public $head = 1; public function tell(){ echo $this->age,'<br />'; echo self::$head,'<br />'; } static public function sayMoney(){ echo $this->money,'<br />'; }</code>
}
class B extends A{
<code> public $age = 22; private $money = 10; public function subtell(){ parent::tell(); echo $this->age,'<br />'; } public function subMoney() { parent::sayMoney(); echo $this->money,'<br />'; }</code>
}
$b = new B();
$b->subtell();//22 1 22;
echo '
The last sentence reports an error Using $this when not in object context
But when subMoney() is called, $this is not bound, $this points to the b object, and then executed parent::sayMoney(); is not bound to $this because it is called statically. Shouldn't it get 2000 when sayMoney() is executed? Why does it report an error? Is it different from the previous $b->subtell(); call? Same
This cannot be used in static methods. When static properties and methods are created, there may not be any instances of this class that can be called. You can classA::staticMethod()
or $a = new classA(); $a->staticMethod()
, but it cannot be used internallythis
The person above is right, the second function called uses static, so $this cannot be used
private indicates that it is private and only allows access within the class, even if it is an inherited class.
If inherited access is allowed and you don’t want external access, you can change it to: protected.
In addition, the instance of that class is created, and $this represents which class.