Re: this oop-script still doesn't work
| From: | Nick Eby | Date: | Tue, 19 Nov 2002 23:41:45 +0000 |
| Subject: | Re: this oop-script still doesn't work | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-125162@lists.php.net to get a copy of this message | ||
seems to me that you're trying to have an object's member behave as if it
had global scope. you never say "$foo = new bar()". instead you say
"$this->foo = new foo()" where $this is an instance of Overall. you can't
instantiate a Foo as a member of an Overall instance, and then expect to
talk to that Foo without going thru the instance itself (which Jonathan
suggested).
so, either use $overall->foo to talk to the Foo instance, or you could do
this in Overall::load():
function load($class)
{
global $$class;
$$class = new $class();
$this->$class =& $$class;
}
which i think will create the Foo instance in global scope, and give the
Overall instance a member that is a reference to that same object.
"Tularis" <tularis@xmbforum.com> wrote in message
news:20021119221514.96610.qmail@pb1.pair.com...
> ok, I have gotten great help here already, but it still doesn't work,
> it returns a object not found error (on the $foo->bar() line). I want to
> change $overall->foo->bar() to $foo->bar... any tips? or help??
>
> thanx!
> - Tularis
>
> <?php
>
> class overall {
> var $loaded;
>
> function load($class){
> eval("global \$$class;"); // This didn't work either
> //$this->loaded[] = $class; // see the comments with the foreach()
> eval("\$this->$class = new $class;");
> return true;
> }
> }
>
> class foo {
> var $bar;
>
> // Constructor
> function bar(){
> if(!isset($this->bar)){
> $this->bar = 1;
> }else{
> $this->bar++;
> }
> echo $this->bar."<br>";
> }
> }
>
> // Start actual loading
> $overall = new overall;
> $overall->load('foo');
>
> /*
> foreach($overall->loaded as $key=>$val){ // didn't work.. I commented
> this out, because I thought it MIGHT interfere with the other way
> $val =& $overall->$val; // both don't work though :(
> }
> */
>
> $overall->foo->bar();
> $overall->foo->bar();
> $overall->foo->bar();
> $overall->foo->bar();
>
> // it doesn't understand this
> $foo->bar();
> ?>
>