Page 1 of 1
Access private member through subclass
Posted: Tue Aug 26, 2008 4:37 pm
by alcu
Hi all,
I have recently started looking at OO PHP and have been playing with some simple code. It appears that you can access private members through a subclass - why is this? Is it a problem with my code or the installation? Here is the code:
class testI {
private $s;
}
class testD extends testI {
}
$test = new testD();
$test->s = "abc";
print $test->s;
$test2 = new testI();
$test2->s = "def";
print $test2->s;
In the testI class, if you change $s from public to protected it behaves as you would expect (accessible through testI and testD when public and neither when protected) but if $s is private then it is accessible through testD which I figure it shouldn't be?
Does anyone else get this?
Re: Access private member through subclass
Posted: Tue Aug 26, 2008 5:57 pm
by Ziq
The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private. Public declared items can be accessed everywhere. Protected limits access to inherited and parent classes (and to the class that defines the item). Private limits visibility only to the class that defines the item. (
Visibility Manual)
First example:
Code: Select all
<?php
class foo
{
private $x;
public function public_foo()
{
echo 'public method';
}
protected function protected_foo()
{
$this->private_foo(); // correct
echo 'protected method';
}
private function private_foo()
{
$this->x = 3; // correct
echo 'private method';
}
}
class foo2 extends foo
{
public function display()
{
$this->protected_foo(); // Correct! PROTECTED declared
$this->public_foo();
// $this->private_foo(); // Not correct! PRIVATE declared
}
}
$x = new foo();
$x->public_foo();
//$x->protected_foo(); // Not correct! PROTECTED declared
//$x->private_foo(); // Not correct! PRIVATE declared
$x2 = new foo2();
$x2->display();
?>
Your example:
Code: Select all
<?
class testI
{
private $s = 'default value';
}
class testD extends testI
{
}
$test = new testD();
$test2 = new testI();
$test->s = "abc";
//$test2->s = "def"; // Fatal error. Not correct private declared
/**
* Variable testD::s and testI::s not same! You can't use private testI::$s in testD class
* If you try use private method will be fatal error
*
* Look!
*/
print_r($test);
print_r($test2);
/**
* You can see [s:private] variable.
*/
?>
Re: Access private member through subclass
Posted: Tue Aug 26, 2008 6:20 pm
by Christopher
alcu wrote:It appears that you can access private members through a subclass - why is this? Is it a problem with my code or the installation?
That's what 'private' means -- the property can only be accessed in the class it is declared in. 'protected' means the class and any class that inherits the class can access the property. 'public' mean that the class, inheriting classes, and external code can access the property.
Re: Access private member through subclass
Posted: Wed Aug 27, 2008 9:27 am
by alcu
Hiya, thanks for your replies.
I understand that private means the property shouldn't be accessible by external code which is the problem I'm having. I can access and change $s (when it is set to private) through an instance of the subclass (testD) which I didn't think I should be able to?
Re: Access private member through subclass
Posted: Wed Aug 27, 2008 9:44 am
by panic!
What version of PHP are you using?
Re: Access private member through subclass
Posted: Wed Aug 27, 2008 3:23 pm
by alcu
Hi, the PHP version is 5.2.5.
Re: Access private member through subclass
Posted: Wed Aug 27, 2008 7:45 pm
by Ziq
alcu wrote:Hiya, thanks for your replies.
I understand that private means the property shouldn't be accessible by external code which is the problem I'm having. I can access and change $s (when it is set to private) through an instance of the subclass (testD) which I didn't think I should be able to?
You can't get access to private method or member through the subclass. This member or method just not inherited. If you want to save access to member ($s) through subclass and disable access through external code, you should use protected prefix.