Jump to content


Photo

How can I call a class from a class


  • Please log in to reply
5 replies to this topic

#1 HGeneAnthony

HGeneAnthony
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 15 March 2006 - 05:23 AM

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:

    class Test {
        private $user = new User("geneanthony", "hea9671");
            
    }

How can I call another class from a class. This wouldn't be an issue if it wasn't in a class.

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 15 March 2006 - 07:48 AM

You can't use new when you're just declaring class variables. Put it in the constructor instead (PHP5 code):

class Test {
   private $user;
   function __construct() {
      $this->user = new User("geneanthony", "hea9671");
   }
}


#3 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 08:33 AM

It works fine for me like this..
class fooooooo {
    public function bar($s1) {
        $foo = new bar($RAWR);
        $foo->method();
    }
}

To each their own [=

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#4 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 15 March 2006 - 08:37 AM

[!--quoteo(post=355272:date=Mar 15 2006, 02:33 AM:name=keeB)--][div class=\'quotetop\']QUOTE(keeB @ Mar 15 2006, 02:33 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It works fine for me like this..
class fooooooo {
    public function bar($s1) {
        $foo = new bar($RAWR);
        $foo->method();
    }
}

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

#5 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 15 March 2006 - 08:47 AM

[!--quoteo(post=355275:date=Mar 15 2006, 08:37 AM:name=greycap)--][div class=\'quotetop\']QUOTE(greycap @ Mar 15 2006, 08:37 AM) View Post[/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.

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#6 HGeneAnthony

HGeneAnthony
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 15 March 2006 - 10:49 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users