Baruch Belete
Baruch Belete

Reputation: 62

PHP - call class from class

Hello i looking for a way to call class function from another class.

i've tried diffrent PHP ways such as the classic way, to call class from class.

http://pastebin.com/X5VfaChr

require_once "user.php";
$user = new UserAction();
class htmloutput{
    public function WebSite(){
        $user->Moshe();
    }
}

Its shows me this error:" syntax error, unexpected 'new' (T_NEW), expecting function (T_FUNCTION)" i dont know about more ways to call a class.

I'll be happy to get helping and learn somethin' from that.

Have Good day, Baruch

Upvotes: 0

Views: 77

Answers (2)

Rafayel A.
Rafayel A.

Reputation: 54

Try to get the instance inside your function, as the following:

require_once 'user.php';
class htmloutput {
    public function WebSite(){
        $user = new UserAction();
        $user->Moshe();
    }
}

Upvotes: 0

Daniel W.
Daniel W.

Reputation: 32260

Besides the comment from Abhik Chakraborty, this fixes the issue coming next:

It's all about scope. Google for DI injection:

require_once "user.php";
$user = new UserAction();

class htmloutput {
    public function WebSite(UserAction $user) {
        $user->Moshe();
    }
}

Upvotes: 1

Related Questions