开发者

how can i use main class variable in extendeded class

class a{

$array = array();

}
class b extends a{

开发者_JAVA百科
**I need to get that array here !**

}

I'm not familiar with oops concept so please somebody help me


   class a
   { 

      public $_array = array(); 

   } 

   class b extends a
   { 

      public function getArray()
      {
         return $this->_array;
      }

   } 


   $x = new b();
   echo $x->_array;
   echo $x->getArray();

And read up on visibility in classes, it'll help understand when something is accessible from a child class, or only from the parent


You just read property as it would be in child class

<?
    class a {
        var $array = array(1,2,3);
    }

    class b extends a {
    }

    $b = new b();
    print_r ($b->array); // prints array
?>

See online.


You need to define your array as public or protected property of your class a

class a {
  protected $array = array();
}

class b extends a {
   public function __construct() {
     $this->array = array('a', 'b', 'c');
   }
}

There are three visibility levels of properties & methods:

  1. public. It means that property is visible outside the class
  2. protected. This property will be visible in the class and its children (class that extend this one)
  3. private. This property will be visible only from the class where property is defined.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜