Reply to Re: Access private props through ancestor

Your name:

Reply:


Posted by Jerry Stuckle on 04/02/07 18:43

rick@fourfront.ltd.uk wrote:
> I have a situation where I want to write an extensible class that is
> 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
>

No, that's the purpose of private properties - to hide your implementation.

The correct way to do this would be for B to have its own Push and Pop
functions which save/restore its data, and call the Push and Pop
functions in A to save and restore its data.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация