PHP classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    PHP classes

    Hi,

    If I have a class that defines another class, how do i access the sub one from the main code.

    So I have something like:
    <?
    Code:
    class A{
       function A($foo){
           $this->p = new B($foo);  
    
       }
    
    }
    
    class B{
        function Z($myvar){
          $this->ID= $myvar;
            
       }
    
    }
    
    ?>
    But how would the main code go to access the ID property of B when an A has been defined... i.e. not like this:
    Code:
    $newvar = new A(666);
    echo "B=".$newvar->p->ID;
    how should it look?

    thanks

    Andy
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Andy.

    The code you provided should work, since undeclared class members are assumed to be public in PHP.

    A slightly more sane-looking approach might look something like this:

    [CODE=PHP]
    class A
    {
    public $child;

    public function __construct( $subVal )
    {
    $this->child = new B($subVal);
    }
    }

    class B
    {
    public $id;

    public function __construct( $id )
    {
    $this->id = $id;
    }
    }

    $newVar = new A(666);
    echo "B = {$newVar->child->id}";
    [/CODE]

    Alternatively, you might want A to be a wrapper for B. In this case, you might consider using magic methods to make the child class accessible:

    [CODE=PHP]
    class A
    {
    protected $child;

    public function __construct( $subVal )
    {
    $this->child = new B($subVal);
    }

    public function __get( $var )
    {
    if( isset($this->child->$var) )
    {
    return $this->child->$var;
    }
    else
    {
    return null;
    }
    }
    }

    class B
    {
    public $id;

    public function __construct( $id )
    {
    $this->id = $id;
    }
    }

    $newVar = new A(666);
    echo "B = {$newVar->id}";
    [/CODE]

    In the second example, PHP will look for A::$id, which doesn't exist, so it will instead call A::__get('id'), which will check to see if B::$id has been set.
    Last edited by pbmods; May 29 '08, 12:38 AM. Reason: Fixed a syntax error.

    Comment

    • theS70RM
      New Member
      • Jul 2007
      • 107

      #3
      Thanks, that helps alot. I'll have a look at applying this to my code.

      Comment

      • theS70RM
        New Member
        • Jul 2007
        • 107

        #4
        a couple of questions..


        Code:
        class A
        {
          public $id;
        
          public function __construct( $id )
          {
            $this->id = $id;
          }
        }

        how come function __construct(){} doesnt work for me,

        how come I cant declare public functions or variables?

        Cheers

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          You are probably running PHP 4, which does not support these features.

          Check out this document to see what features are available.
          Last edited by pbmods; May 29 '08, 11:36 PM. Reason: Removed experimental linktext.

          Comment

          • theS70RM
            New Member
            • Jul 2007
            • 107

            #6
            oh ok,

            Yea i am using php4 and cant upgrade at the mo.

            cheers

            Andy

            Comment

            Working...