View Full Version : calling an outer class from an inner class
thesavior
03-20-2007, 06:17 PM
okay, so i have a class called $superclass
inside superclass, i call and initialize another class called $tpl
($superclass->tpl)
superclass contains a few different classes.
Inside tpl i need to use another class that superclass is including. How do I do this?
thesavior
03-21-2007, 12:43 AM
here is what I have tried so far:
When i define tpl in superclass, I did it like this:
require_once(ROOT_PATH."includes/functions/tpl_parse.php");
$this->tpl = new tpl_parse(); //Set Language class
$this->tpl->superclass =& $superclass; //Allow superclass usage inside tpl class
And in the tpl class, i tried:
$this->superclass->err->errno = 2;
$this->superclass->err->throwerr();
the two lines above work outside of the tpl class when called just starting with $superclass. How come I cant use those inside tpl?
thesavior
03-21-2007, 05:32 AM
the error im getting with the above code is:
Fatal error: Call to undefined method stdClass::throwerr() in /mounted-storage/home45c/sub001/sc21473-GRUR/dev/forum/includes/functions/tpl_parse.php on line 52
ralph l mayo
03-21-2007, 05:47 AM
If it really is a superclass/subclass relationship (ie, the subclass IS-A superclass), you should just extend the superclass:
class Super
{
protected function doSuperStuff() { echo 'asdf'; }
}
class Sub extends Super
{
public function Sub2()
{
$this->doSuperStuff();
}
}
$test = new Sub(); // => asdf
If the superclass HAS-A subclass you could just pass it either to the initializer or to a setter method:
class Sub
{
private $super;
public function Sub(&$super = null)
{
if (isset($super)) $this->set_super($super);
}
public function set_super(&$super)
{
$this->super = $super;
$super->doSuperStuff();
}
}
class Super
{
private $sub;
public function Super()
{
$this->sub = new Sub($this);
}
public function doSuperStuff() { echo 'asdf'; }
}
$test = new Super(); // => asdf
PHP has historically had problems with circular references, I don't know if it's better now.
thesavior
03-21-2007, 05:59 AM
why is that better than:
class superclass
{
public $vars = array(); //Superclass variable holder
public $DB; //Superclass database holder
public $err; //Superclass error holder
public $lang; //Superclass Language holder
//constructor
function run()
{
//Initialize database connection
require_once(ROOT_PATH."includes/functions/dconnect.php");
//Add the DB class to the super
$this->DB = new dconnect();
// Include Error Function
require_once(ROOT_PATH."includes/functions/err.php");
$this->err = new err(); //Set error class
// Include Language Function
require_once(ROOT_PATH."includes/functions/languages.php");
$this->lang = new lang(); //Set Language class
require_once(ROOT_PATH."includes/functions/tpl_parse.php");
$this->tpl = new tpl_parse(); //Set Language class
$this->tpl->superclass =& $superclass; //Allow superclass usage inside tpl class
}
}
What is different?
ralph l mayo
03-21-2007, 07:12 AM
You shouldn't write data directly into classes from outside because it breaks encapsulation.
edit: thought it didn't work but it seems ok in my tests. above still applies though /\/\/\
thesavior
03-21-2007, 02:37 PM
There has got to be a way to do what I need to do without drastically changing my classes around. Im way to deep to do that now.
KeZZeR
03-21-2007, 02:41 PM
If a class is nested within another one and you're having issues with passing data then you should perhaps modularise your design a bit more. Typically nested classes are avoided most of the time and it's better to have the nested class as a completely separate class.
Also, superclass isn't very descriptive, you should call it something with relates to the overall objective of the class perhaps?
you haven't defined $superclass as anything. You probably want to be using $this.
aedrin
03-21-2007, 02:45 PM
$this->tpl->superclass =& $superclass;
If you are using PHP 5+, please remove that &.
thesavior
03-22-2007, 01:14 AM
hmm, i apparently needed to do:
$this->tpl->superclass = $this;
Thanks for the help everyone.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.