Jump to content

How can I call a class from a class


HGeneAnthony

Recommended Posts

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 13

Here'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.
Link to comment
https://forums.phpfreaks.com/topic/5009-how-can-i-call-a-class-from-a-class/
Share on other sites

[!--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().
[!--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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.