Jump to content

Protected Member Access


atitthaker

Recommended Posts

<?

class A

{

protected $b=20;

private $a=10;

protected function access()

{

print("here");

}

}

class B extends A

{

public function test()

{

$this->access() ;

}

 

}

class C extends B

{

function temp()

{

$this->access();

}

}

 

$abc= new C();

$abc->temp();

?>

 

I have above piece of code...

 

When I am executing it, it generates output "here"...

 

Now as the statement is inside a protected method of class A, which is directly not accessible by class C as it is not inheriting class A directlty(through class B though). This script is suppose to generate error, and protected method of class A shall not be accessible by class C. But it is accessing it, and printing value...

 

Why it is so??? Isn't it violation of OOP concept??

Link to comment
https://forums.phpfreaks.com/topic/40660-protected-member-access/
Share on other sites

As far as I know and understand, according to OOP concept, protected member shall be accessible to it's next level child but not further.

 

So in the example below, it shall be accessible to class B but not class C.

 

In child class protected member's visibility shall reduce to private and shall not be accessible in further children...

 

Isn't it the case??

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.