In this tutorial we will see the visibility of the properties/methods of Objects of the same class, wherein same type of objects can access each other's protected/private members and also both the instances are not same.
Let us create a file test.php
Example
class testObject
{
private $abc;
public function __construct($xyz)
{
$this->abc=$xyz;
}
private function bar()
{
echo 'This is a private method of any object that belongs to the class testObject.';
}
public function baz($any)
{
//Here we are changing the value of private property of an object:
var_dump($any->abc);echo '<br>';
$any->abc='Hello World ';
var_dump($any->abc);echo '<br>';
//Another object $any calls the private function of class testObject
$any->bar();
}
}
$test = new testObject('xyz123'); //parameter passed to the constructor of class testObject
//Here an object "$test" calls the public function "baz" wherein we call the private function of another object i.e. "$any" of same class.
$test->baz(new testObject('other123'));
OUTPUT:
string(8) "other123"
string(12) "Hello World "
This is a private method of any object that belongs to the class testObject.
0 Comment(s)