Reputation: 2797
I want to convert from a string to Class name in PHP as below script. But i got errors Class name must be a valid object or a string it may be error because tables still a string.
$table = Teller::select('*')->where('user_id','=', $this->user_id)->first();
$modelName = trim($table->tables,'"');
$loan = $modelName::select('*')->where('id','=', $id)->get();
Upvotes: 2
Views: 6225
Reputation: 72226
It is possible to convert from a string to class name
There is no class name notion in PHP.
You have a string, if there is a class with that name then you can use the string to create objects of the class or call static methods of the class.
The code you posted looks correct to me. I even wrote a small class and tested similar code and it works fine. There are two reasons I can see that make the code fail with PHP errors:
$modelName
is not a string; this is out of the question for the code you posted. $modelName
is initialized with the value returned by trim()
and trim()
always returns strings.$modelName
contains only the class name but the posted code is part of a function or method declared in a namespace; in this case $modelName
must contain the full name of the desired class, starting from the root namespace; the initial \
is not required in the full class name but it can be used.This requirement to use the complete names for classes sounds like a quirk of the interpreter but it is not; the use
declaration is just a shortcut that lets the programmer use shorter names in the code. The use
declaration doesn't produce any code, during runtime all the class names are fully qualified with their namespaces.
A quick demo can be found at https://3v4l.org/lg2qC
Upvotes: 2
Reputation: 7617
Consider this Simple Scenario:
<?php
class SomeClass{
public static $a = 12;
public static $b = "some value";
public static $c = "another value";
public static function getSomeData(){
return self::$a . " " . self::$b . " " . self::$c;
}
}
$b = SomeClass::getSomeData();
//DUMPS '12 some value another value' TO THE OUTPUT STREAM...
var_dump($b);
$strClassName = "SomeClass";
//STILL DUMPS '12 some value another value' TO THE OUTPUT STREAM...
var_dump(call_user_func($strClassName. "::getSomeData"));
Extending this Knowledge to your Unique Case, You might want to do something like:
<?php
$table = Teller::select('*')->where('user_id','=', $this->user_id)->first();
$modelName = trim($table->tables,'"');
$implicitCall = call_user_func($modelName. "::select", '*');
$implicitCall = call_user_func($modelName. "::where", array('id', '=', $id));
$loan = call_user_func($modelName. "::get");
?>
Optionally; You may even take this a little further. Since we know that you are using Fluent Setters; it is clear that the First implicit call will return an instance of the Class so we could do something like so:
<?php
$table = Teller::select('*')->where('user_id','=', $this->user_id)->first();
$modelName = trim($table->tables,'"');
// THIS SHOULD RETURN AN INSTANCE OF THE CLASS IN QUESTION: THE MODEL CLASS
$implicitCall = call_user_func($modelName. "::select", '*');
// DO YOU DOUBT IT? WELL, DOUBT IS THE BEGINNING OF ALL KNOWLEDGE.
// I DOUBT IT TOO; SO LET'S CONFIRM OUR DOUBTS
var_dump($implicitCall); // EXPECTED TO DUMP THE CLASS IN QUESTION.
// NOW WE CAN JUST USE THE $implicitCall VARIABLE AS IF IT WAS AN INSTANCE OF THE MODEL CLASS LIKE SO:
$loan = $implicitCall->where('id','=', $id)->get();
?>
I hope this answers helps and works for you though... ;-)
Upvotes: 2