Access private props through ancestor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rick@fourfront.ltd.uk

    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

  • Jerry Stuckle

    #2
    Re: Access private props through ancestor

    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@attgl obal.net
    =============== ===

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: Access private props through ancestor

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

      Comment

      • rick@fourfront.ltd.uk

        #4
        Re: Access private props through ancestor

        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].

        Comment

        Working...