corillo181 Posted November 7, 2007 Share Posted November 7, 2007 how come my var array is returned Empty? <?php class displayA{ protected $db; public $name = array(); public $info = array(); public $img = array(); function __construct(){ $this->db = new DB(); } function addToArray(){ $this->name[] = "juan"; } function getNames(){ var_dump($this->name); } } class allArtist extends displayA { function __construct(){ parent::__construct(); } function getName(){ parent::getNames(); } } $ini = new displayA(); $ini->addToArray(); $show = new allArtist(); $show->getName(); // RETURN EMPTYS ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 well no need of overidding function getName(){ parent::getNames(); } Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 7, 2007 Author Share Posted November 7, 2007 OVER RIDDING? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 Oh I see sorry mistake in seeing parent::getNames(); $this->getNames(); Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 7, 2007 Author Share Posted November 7, 2007 are you sure you have any experience with classes? anyways it worked this way <?php include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php'); class displayA{ protected $db; public $name = array(); public $info = array(); public $img = array(); function __construct(){ $this->db = new DB(); } function addToArray(){ $this->name[] = "juan"; } function getNames(){ return var_dump($this->name); } } class allArtist extends displayA { function __construct(){ parent::__construct(); } function getName(){ parent::addToArray(); parent::getNames(); } } $show = new allArtist(); $show->getName(); ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 Yeah a little bit but please read this before you proceed http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 7, 2007 Share Posted November 7, 2007 also good example and usage is as follows http://www.phpfreaks.com/tutorials/150/1.php#1.4 Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 You've created 2 DIFFERENT objects... $ini = new displayA(); $ini->addToArray(); $show = new allArtist(); $show->getName(); // RETURN EMPTYS $ini has inside it the array of names of which Juan is one. however $show (which extends displayA NOT $ini) has an array of names inside it, which is empty, because you haven't added any... Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 What you SHOULD be doing is : $show = new allArtist(); $show->addToArray(); $show->getNames(); You don't even need the getName() function in allArtist... Quote Link to comment Share on other sites More sharing options...
corillo181 Posted November 7, 2007 Author Share Posted November 7, 2007 oh understand now. thank you aschk Quote Link to comment Share on other sites More sharing options...
aschk Posted November 7, 2007 Share Posted November 7, 2007 For any more object oriented help try the specific OOP help part of the forum. I like hanging around there Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.