Re: Access private props through ancestor
- From: Schraalhans Keukenmeester <bitbucket@xxxxxxxxxxxx>
- Date: Tue, 03 Apr 2007 02:17:14 +0200
rick@xxxxxxxxxxxxxxxx wrote:
I have a situation where I want to write an extensible class that isIf keeping the variable shielded from client code is most important to
capable of saving / restoring properties of classes derived from it.
A simplified example is explained as follows;-
class A
{
private $Save ;
public function Push()
{
$this->Save = serialize( get_object_vars( $this ) ) ;
}
public function Pop()
{
foreach( unserialize( $this->Save ) as $Prop => $Value )
$this->$Prop = $Value ;
}
}
class B extends A
{
private $Priv ;
public function SetPriv( $Num )
{
$this->Priv = $Num ;
}
public function GetPriv()
{
return $this->Priv ;
}
}
$C = new B ;
$C->SetPriv( 1 ) ;
$C->Push() ;
$C->SetPriv( 2 ) ;
echo $C->GetPriv()."<br/>\n" ;
$C->Pop() ;
echo $C->GetPriv()."<br/>\n" ;
At the point of the call to $C->Pop(), I get the following error;-
Cannot access private property B::$Priv
It is only private properties which cause this problem, public and
protected properties defined in B will save and restore as intended.
Is there a way around this ?
TIA
you in this particular case, consider leaving them public, write a
custom __get() and __set() accessors and catch attempts at meddling with
$priv there. Not completely 'as it should' but it does the job.
Or define the (protected) variable in the parent class instead and
access it through parent::$priv.
I know protected vars in the parent class ARE visible to its children,
but I haven't got a clue whether this also goes the other way around.
.
- Follow-Ups:
- Re: Access private props through ancestor
- From: rick
- Re: Access private props through ancestor
- References:
- Access private props through ancestor
- From: rick
- Access private props through ancestor
- Prev by Date: Remote PHP
- Next by Date: Re: Best Practice - Constant Page Layouts
- Previous by thread: Re: Access private props through ancestor
- Next by thread: Re: Access private props through ancestor
- Index(es):
Relevant Pages
|