HGeneAnthony Posted March 15, 2006 Share Posted March 15, 2006 I created a class (Users) which I would like to call an instance from in another class. However, I'm getting the error:PHP Parse error: syntax error, unexpected T_NEW in C:\www\PHPTestProject\index.php on line 13Here's the conflicting code:[code] class Test { private $user = new User("geneanthony", "hea9671"); }[/code]How can I call another class from a class. This wouldn't be an issue if it wasn't in a class. Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/ Share on other sites More sharing options...
wickning1 Posted March 15, 2006 Share Posted March 15, 2006 You can't use new when you're just declaring class variables. Put it in the constructor instead (PHP5 code):[code]class Test { private $user; function __construct() { $this->user = new User("geneanthony", "hea9671"); }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/#findComment-17726 Share on other sites More sharing options...
keeB Posted March 15, 2006 Share Posted March 15, 2006 It works fine for me like this..[code]class fooooooo { public function bar($s1) { $foo = new bar($RAWR); $foo->method(); }}[/code]To each their own [= Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/#findComment-17739 Share on other sites More sharing options...
greycap Posted March 15, 2006 Share Posted March 15, 2006 [!--quoteo(post=355272:date=Mar 15 2006, 02:33 AM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 02:33 AM) [snapback]355272[/snapback][/div][div class=\'quotemain\'][!--quotec--]It works fine for me like this..[code]class fooooooo { public function bar($s1) { $foo = new bar($RAWR); $foo->method(); }}[/code]To each their own [=[/quote]Keeb, yours isnt the same. In the other two examples, the variable is a member of the class. Your $foo is local to bar(). Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/#findComment-17742 Share on other sites More sharing options...
keeB Posted March 15, 2006 Share Posted March 15, 2006 [!--quoteo(post=355275:date=Mar 15 2006, 08:37 AM:name=greycap)--][div class=\'quotetop\']QUOTE(greycap @ Mar 15 2006, 08:37 AM) [snapback]355275[/snapback][/div][div class=\'quotemain\'][!--quotec--]Keeb, yours isnt the same. In the other two examples, the variable is a member of the class. Your $foo is local to bar().[/quote]That is correct.. I was offering my alternative way to offer the same solution.. I cannot see the benefit, other than object sharing during method execution.. from making it a member of the class. Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/#findComment-17746 Share on other sites More sharing options...
HGeneAnthony Posted March 15, 2006 Author Share Posted March 15, 2006 Thank you for the help! It worked! Eclipse has a real nice PHP IDE that tells you of any errors as you save it. It offers a lot of cool features and I would recommend checking it out. Quote Link to comment https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/#findComment-17758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.