Welcome to WebmasterWorld Guest from 54.147.0.174

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Class within a class

     

mafaz

11:54 am on Jun 16, 2009 (gmt 0)

5+ Year Member


Is it possible to access a method of a class which is an object of a second class, from the second classes object.

that is
class a{
public method A
}

class b{
$vA = new a();
}

$vB = new b();

i want to access method A from $vB. How can i do this?

Thanks in advance

coopster

3:29 pm on Jun 16, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, as long as the properties and methods have the correct visibility.
$vB->vA->A();

mafaz

3:10 am on Jun 17, 2009 (gmt 0)

5+ Year Member



Coopster, I cant seem to figure this out. Its generating an error. Here is my code please tel me if I've done something wrong

class super{
private $a = "Test";

public function getA(){
return $this->a;
}
}

class sub{
public $get = new super();
}

$obj = new sub();
echo ($obj->get->getA());

The error that is being displayed is

Parse error: syntax error, unexpected T_NEW in #*$! on line 11

coopster

12:46 pm on Jun 17, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



A class property/member default value must be a constant expression, not (for example) a variable, a class member or a function call. See Example 3:
[php.net...]

So you need to do something like this:

class super { 
private $a = "Test";
public function getA(){
return $this->a;
}
}
class sub {
public $get;
public function __construct()
{
$this->get = new super();
}

}
$obj = new sub();
echo ($obj->get->getA());

mafaz

8:03 am on Jun 18, 2009 (gmt 0)

5+ Year Member



Thanks mate...I get it now! Thanks
 

Featured Threads

Hot Threads This Week

Hot Threads This Month