0

I working in Laravel project and I can't call static method across variable For example:

$objName = 'User';
$objName::get();

On this way I get error.

4
  • 'user' is a string, not an object Commented Jan 29, 2019 at 14:59
  • @noid, in php you can call Class with string variable Commented Jan 29, 2019 at 15:01
  • I get error. which is? Commented Jan 29, 2019 at 15:08
  • You are correct, i need more coffee... Commented Jan 29, 2019 at 15:08

3 Answers 3

1
$objName = 'User';

Is a string, for using the get() method $objName should be an object, for example:

$objName = User::all()->first(); // this will return an object
Sign up to request clarification or add additional context in comments.

Comments

1

Ok I use

User::all();

But I want get parametar from URL for example www.example.com/User, www.example.com/Articles -> User and Article is parametar in URL (this is Laravel web route) and call static method. When I wrtie first URL than call User object if I write first URL than call Article object.

www.example.com/User

$param= 'User';
$param::all();

www.example.com/Article

$param= 'Article';
$param::all()

Comments

0

I find solution! This solution in Laravel:

$data = call_user_func( array('\App\\'.$param , 'all'));

but if you want use in plain PHP then is:

$data = call_user_func( array($param , 'all'));

This will call Object and Method.

If you want to send arg in methond thent is:

$data = call_user_func( array('\App\\'.$param , 'all'), $arg);  /*For Laravel*/
$data = call_user_func( array($param , 'all'), $arg);  /*For plain PHP*/

Comments

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.