In the below program the last statement echo $objb->test; should not output any value. The second last statement should throw an error saying that private member cannot be accessed. However, I am getting output 20 when running the program.
Since PHP is loosely type language is the $objb create a new variable outside the class as well...
<?php
error_reporting(E_ALL);
class A {
public $a;
private $test;
protected function sayhello(){
echo "<p>hello class A</p>";
}
}//end of class A
class B extends A {
//private $c;
public function sayhellonew(){
$this->sayhello();
echo "<p>hello class B</p>";
}
}//end of class B
$objb = new B();
$objb->sayhellonew();
$objb->a = 10;
echo $objb->a;
echo "<br>";
$objb->test = 20;
echo $objb->test;
?>
Output I am getting output 20 why is it working?
Example Explained Here, we declare a static property: $value. Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).
The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties: $a = array('123' => '123', '123foo' => '123foo'); $o = (object)$a; $a = (array)$o; echo $o->{'123'}; // error!
Definition and Usage. The instanceof keyword is used to check if an object belongs to a class. The comparison returns true if the object is an instance of the class, it returns false if it is not.
Since the $test
property is private to A
, it is not visible in the scope of B
. That means that B
can have its own property named $test
. When you assign objb->test = 20
, that's what you're setting.
The following shows that you're not actually setting the private variable:
class A {
public $a;
private $test = 10;
protected function sayhello(){
echo "<p>hello class A</p>";
}
public function showTestA() {
echo "Test in A = " . $this->test . '<br>';
}
}//end of class A
class B extends A {
//private $c;
public function sayhellonew(){
$this->sayhello();
echo "<p>hello class B</p>";
}
public function showTestB() {
echo "Test in B = " . $this->test . '<br>';
}
}//end of class B
$objb = new B();
$objb->test = 20;
$objb->showTestA();
$objb->showTestB();
This will display:
Test in A = 10
Test in B = 20
Because, you are overriding the parent class' private property with childs class' public property.
It is valid in PHP.
Try adding a new private property $c
in class B
and try to override it.
You will get an error: Fatal error: Cannot access private property B::$c
Check it here: http://codepad.org/74IgLqYn
Because its not the test
same variable that you have declare in it's parent(A) class. In fact its will treat it as public $test;
of class B. Check below you will get the issue.
<?php
error_reporting(E_ALL);
class A {
public $a;
private $test;
protected function sayhello(){
echo "<p>hello class A</p>";
}
public function setTest(){
$this->test = "50";
}
}//end of class A
class B extends A {
//private $c;
public function sayhellonew(){
$this->sayhello();
echo "<p>hello class B</p>";
}
}//end of class B
$objb = new B();
$objb->sayhellonew();
$objb->a = 10;
echo $objb->a;
echo "<br>";
$objb->setTest();
echo $objb->test;
?>
CODEPAD.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With