Jump to content

about parent members


Liquid Fire

Recommended Posts

I know that php classes are not exactly the same as other languages classes(like C++) so i have a question.  Let say i have this code:

<?php
class a
{
    protected function test()
    {
        print_r($this->_data_map);
    }
    
    protected $_data_map;
}

class b extends a
{
    public function __contruct()
    {
        parent::$_data_map = array("a" => "b");
    }
    
    public function test()
    {
        parent::test();
    }
}

$a = new a();
$b = new b();

$b->test();
?>

My question is will class a have access to the data created by class b's constructor?

Link to comment
Share on other sites

OK, I figured it out.

 

First..

 

    public function __contruct()

 

Should be

 

    public function __construct()

 

That was annoying! I usually use the name of the class for the constructor. In this case, it would be class a { public function a() {}

 

Anyway..

 

You forgot to declare the functions static, since you're accessing them in a static way.

 

This works:

 

<?php
class a
{
    protected static function test()
    {
        print_r(self::$_data_map);
    }
    
    protected static $_data_map;
}

class b extends a
{
    public function __construct()
    {
        parent::$_data_map = array("a" => "b");
    }
    
    public static function test()
    {
        parent::test();
    }
}

$a = new a();
$b = new b();

$b->test();




?>

Link to comment
Share on other sites

I don't believe parent classes (in your example, class a) can have knowledge of their child classes (class b).  Child classes, however, can have knowledge (properties and methods) of their parent class.  So, if you have a super class that is the basis of all your datamap stuff, the child classes will have the same types of properties and methods as those that are either public or protected in the parent class.

 

So:

<?php

class DataMapper{
   protected $dataMap;

   protected function test(){
      print_r($this->dataMap);
   }
}

class UniqueMapper extends DataMapper{
   public function __construct(){
      $this->dataMap = array("a" => "b");
   }
}

class ComplexMapper extends DataMapper{
   public function __construct($key, $value){
      $this->dataMap = array($key => $value);
   }
}

$unique = new UniqueMapper();
$complex = new ComplexMapper("name", "Bubba");

$unique->test(); //prints a => b
$complex->test(); //prints name => Bubba

?>

 

In this example, even though all classes have a $dataMap property, it's not the same exact one.  Extending a class/using inheritence doesn't copy data.  It just tells the child class that part of its mold came from the parent class.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.