Access private member through subclass

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
alcu
Forum Newbie
Posts: 3
Joined: Tue Aug 26, 2008 4:20 pm

Access private member through subclass

Post 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?
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Access private member through subclass

Post 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.
 */
?>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Access private member through subclass

Post 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.
(#10850)
alcu
Forum Newbie
Posts: 3
Joined: Tue Aug 26, 2008 4:20 pm

Re: Access private member through subclass

Post 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?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Access private member through subclass

Post by panic! »

What version of PHP are you using?
alcu
Forum Newbie
Posts: 3
Joined: Tue Aug 26, 2008 4:20 pm

Re: Access private member through subclass

Post by alcu »

Hi, the PHP version is 5.2.5.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Access private member through subclass

Post 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.
Post Reply