Page 1 of 1
Member Functions and Non Objects
Posted: Fri Jan 05, 2007 6:55 pm
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;
}
}
}
Posted: Fri Jan 05, 2007 7:01 pm
by Luke
I don't see anything wrong with that (assuming Some_Other_Class has a method called boo())
Posted: Fri Jan 05, 2007 7:10 pm
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.
Posted: Fri Jan 05, 2007 7:12 pm
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;
}
}
}
Posted: Fri Jan 05, 2007 7:28 pm
by Ollie Saunders
No it won't work then. This is called "late binding"
Posted: Fri Jan 05, 2007 7:55 pm
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

Posted: Fri Jan 05, 2007 8:21 pm
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";
}
}
}
Posted: Fri Jan 05, 2007 8:32 pm
by Luke
I'm a little confused myself.

Posted: Fri Jan 05, 2007 9:10 pm
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?
Posted: Fri Jan 05, 2007 10:03 pm
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;
}
Posted: Fri Jan 05, 2007 10:06 pm
by blacksnday
Ahhh thanks!
btw, how is it different to do it in php5?
Posted: Sat Jan 06, 2007 4:57 am
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();
}
}