0

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

1
  • 2
    check if the class file user.php is closed i.e. the last } first. Commented Apr 13, 2014 at 20:19

2 Answers 2

1

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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Can i put $user = new UserAction(); inside the class and not in function. because i call him for few questions?. Thanks.
0

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();
    }
}

2 Comments

Can i put $user = new UserAction(); inside the class and not in function. because i call him for few questions?. Thanks.
You can make it to be initialized in constructor, and share between all the functions of the class, for instance: require_once 'user.php'; class htmloutput { public $user; public function __construct() { $this->user = new UserAction(); } public function WebSite(){ $this->user->Moshe(); } public function otherAction() { $this->user->something(); } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.