473,568 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Access private props through ancestor

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

Apr 2 '07 #1
3 1880
ri**@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.
js*******@attgl obal.net
=============== ===
Apr 2 '07 #2
ri**@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
If keeping the variable shielded from client code is most important to
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.
Apr 3 '07 #3
On 3 Apr, 01:17, Schraalhans Keukenmeester <bitbuc...@inva lid.spam>
wrote:
>
If keeping the variable shielded from client code is most important to
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.
What is most important to me is writing a class which can be inherited
by any class in my project - regardless of the type of access levels
of it's properties.
Or define the (protected) variable in the parent class instead and
access it through parent::$priv.
Then the parent class is no longer generic.
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.
Protected variables in the child are visible to and settable by the
parent - it is only the private variables in the child which cause a
problem in this case.
Interestingly, the private variables in the child ARE visible to the
parent - as the Push() function detailed above does successfully save
the serialized details of the child into it's own 'Save' variable.
The problem is only that the Pop function cannot then set the private
variable of the child class.
Surely this behaviour is inconsistent - either the parent can access
private child properties or it can't ?

If anyone can let me know how to write this generic push/pop
functionality that can be inherited by ANY class in my system, please
let me know. This would also be useful for other functionality - i.e.
object caching [having a base caching class that can be inherited by
ANY class to allow the class to fully handle it's own caching
automatically].

Apr 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
3040
by: Arno R | last post by:
Hi there, I just distributed an application in which I (try to) change db.properties depending on CurrentUser() For instance I set the property's AllowBypassKey and AllowBuiltinToolbars to False when CurrentUser() <> "xx" I just noticed that the property's can only be changed changed by a user belonging to 'Admins'. I never noticed this...
4
1992
by: mcca68 | last post by:
hi i'm trying to insert new and updated fields into my onlinereg2 table from datatransfer table (imported weekly into access), though i am running into problems. when i click run, i receive an input box "enter parameter value" of onlinereg2.updated field. i just wanted to update and/or append new fields when the "DataTransfer.updated <>...
14
2551
by: dixie | last post by:
What is the best way to implement a front end for an access database via a web browser on a private intranet? This is totally new ground for me. I want to allow users to enter information into a database and get reports via the normal browser interface. How secure is this? If the browser has access to the access application, do the...
11
2513
by: John Bailo | last post by:
Is anyone familar with the correct syntax for using ancestor:: ? I am trying to use it in a c# application, and I am also testing it in a parser application called Cooktop ( http://www.xmlcooktop.com ) I cannot get it or my c# code to return a node list when using ancestor. Is ancestor:: something that .NET does or does not support? ...
4
1544
by: maya | last post by:
pls what is order of font-props when not specifying names of properties in specification can I assume this is the order: http://www.w3.org/TR/REC-CSS2/fonts.html#font-properties i.e., can I do font verdana italic 12px?
3
1637
by: Luis P. Mendes | last post by:
Hi, I have the following problem: I instantiate class Sistema from another class. The result is the same if I import it to interactive shell. s = Sistema("par") class Sistema:
6
4401
by: karthikbalaguru | last post by:
Hi, Could someone here tell me some links/pdfs/tutorials to know about the difference between Private Inheritance and Public Inheritance ? I am unable to get info w.r.t it. Thx in advans, Karthik Balaguru
5
10996
by: Bubo.virginianus | last post by:
Heya! I feel I'm beating an old horse to death with this question but I can't get around the problem. I want to update a text field on a form with a string from a VB module. I need enter/linefeeds in the string. I've tried: Chr(10) & Chr(13)
1
3421
by: Les Caudle | last post by:
I've got code that's been working for years. This is running on Win2003 Server whiled logged in as the admin. When the MS sec fixes were installed last night, this code now fails with: System.Net.Sockets.SocketException: An attempt was made to access a socket in a way forbidden by its access permissions IDictionary props = new...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.