Member Functions and Non Objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Member Functions and Non Objects

Post by blacksnday »

With the below example, (on php4) an error will be thrown.
More namely the error would be:
Call to a member function boo() on a non-object
Now I could make $foo a global to make the error go away, however I am trying to do this properly.
If globals aren't proper, what would be the proper way to accomplish the below idea?

Code: Select all

class Some_Class {

     function Some_Class()
     {
          $this->foo = new Some_Other_Class;
     }

     function some_something()
     {	
          if($this->foo->boo())
          {
	     return true;
          }
     }
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I don't see anything wrong with that (assuming Some_Other_Class has a method called boo())
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm with Ninja. With the assignment in the constructor there is no way (apart from perhaps another assignment later) that $this->foo could end up a non-object.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

The Ninja Space Goat wrote:I don't see anything wrong with that (assuming Some_Other_Class has a method called boo())
What if you are extending?

Code: Select all

$willitwork = new Some_Class_Ext;
$willitwork->some_something();

class Some_Class_Ext extends Some_Class { 

     function Some_Class_Ext() 
     {  
         //just example
     } 
}

class Some_Class { 

     function Some_Class() 
     { 
          $this->foo = new Some_Other_Class; 
     } 

     function some_something() 
     {  
          if($this->foo->boo()) 
          { 
             return true; 
          } 
     } 
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

No it won't work then. This is called "late binding"
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

ole wrote:No it won't work then. This is called "late binding"
how would I be able to accomplish the idea then?
I have been using and teaching myself classes for some time now,
and now I am getting into using extensions hence some more learning needed now :P
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I am not sure what does not work? Are you trying to call it statically? I just ran the following on a 4.3 box and it worked fine.

Code: Select all

<?php
$willitwork = new Some_Class();
$willitwork->some_something();

class Some_Other_Class {

     function Some_Other_Class()
     {
         //just example
     }
     function boo()
     {
         return true;
     }
}

class Some_Class {

     function Some_Class()
     {
          $this->foo = new Some_Other_Class;
     }

     function some_something()
     {
          if($this->foo->boo())
          {
             echo "true";
        } else {
                echo "false";
          }
     }
}
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm a little confused myself. :?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

arborint wrote:I am not sure what does not work? Are you trying to call it statically? I just ran the following on a 4.3 box and it worked fine.

Code: Select all

<?php
$willitwork = new Some_Class();
$willitwork->some_something();

class Some_Other_Class {

     function Some_Other_Class()
     {
         //just example
     }
     function boo()
     {
         return true;
     }
}

class Some_Class {

     function Some_Class()
     {
          $this->foo = new Some_Other_Class;
     }

     function some_something()
     {
          if($this->foo->boo())
          {
             echo "true";
        } else {
                echo "false";
          }
     }
}
Yes that does work fine, as said above.

When using extenstions it does not work, as Ole points out.
Now I am trying to figure out how to make the 'idea' work if using extensions.

Code: Select all

$willitwork = new Some_Class_Ext(); 
$willitwork->some_something(); 
 

class Some_Class { 

     function Some_Class() 
     { 
          $this->foo = new Some_Other_Class; 
     } 

     function some_something() 
     { 
          if($this->foo->boo()) 
          { 
             echo "true"; 
        } else { 
                echo "false"; 
          } 
     } 
}

class Some_Class_Ext extends Some_Class 
{ 

     function Some_Class_Ext() 
     {  
         //just example 
     } 
}

class Some_Other_Class { 

     function Some_Other_Class() 
     { 
         //just example 
     } 
     function boo() 
     { 
         return true; 
     } 
}
if I made $foo a global inside each method, then the above would work.
However, that is a nasty way of doing it and I'm trying to learn a proper way to use extends.
Maybe instead of calling externally:

Code: Select all

$willitwork = new Some_Class_Ext; 
$willitwork->some_something();
Call it internally where ever needed?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

In PHP4 you need to do:

Code: Select all

class Some_Class_Ext extends Some_Class
{

     function Some_Class_Ext()
     {
        $this->Some_Class();
         //just example
     }
}
You could also do something like:

Code: Select all

class Some_Class_Ext {

     function Some_Class_Ext()
     {
         $this->_init();
     }

class Some_Class {

     function Some_Class()
     {
          $this->_init();
     }

     function _init()
     {
          $this->foo = new Some_Other_Class;
     }
(#10850)
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Ahhh thanks!

btw, how is it different to do it in php5?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

OK I obviously wasn't clear. If in Some_Class_Ext you override the constructor and $this->foo is no longer assigned and won't exist in methods defined in Some_Class_Ext or methods inherited from Some_Class.
btw, how is it different to do it in php5?

Code: Select all

class Foo
{
    private $_property;
    public function __construct()
    {
        $this->_property = new stdClass();
    }
    public function usesProperty()
    {
        $this->_property->message = 'Hello, world!';
    }
}
class Bar extends Foo
{
    private $_anotherProperty;
    public function __construct()
    {
        parent::__construct();
        $this->_anotherProperty = new stdClass();
    }
}
Post Reply