Jump to content

Archived

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

HGeneAnthony

How can I call a class from a class

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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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 [=

Share this post


Link to post
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().

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.