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 ?> Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/ 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(); } Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386593 Share on other sites More sharing options...
corillo181 Posted November 7, 2007 Author Share Posted November 7, 2007 OVER RIDDING? Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386595 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(); Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386602 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(); ?> Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386606 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 Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386610 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 Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386613 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... Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386614 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... Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386616 Share on other sites More sharing options...
corillo181 Posted November 7, 2007 Author Share Posted November 7, 2007 oh understand now. thank you aschk Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386634 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 Link to comment https://forums.phpfreaks.com/topic/76359-solved-class-extends/#findComment-386673 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.