homepage Welcome to WebmasterWorld Guest from 54.211.230.186
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Class within a class
mafaz

5+ Year Member



 
Msg#: 3934265 posted 11:54 am on Jun 16, 2009 (gmt 0)
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

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



 
Msg#: 3934265 posted 3:29 pm on Jun 16, 2009 (gmt 0)

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

mafaz

5+ Year Member



 
Msg#: 3934265 posted 3:10 am on Jun 17, 2009 (gmt 0)

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

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



 
Msg#: 3934265 posted 12:46 pm on Jun 17, 2009 (gmt 0)

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

5+ Year Member



 
Msg#: 3934265 posted 8:03 am on Jun 18, 2009 (gmt 0)

Thanks mate...I get it now! Thanks

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved