2

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();
2
  • You definitely have a class with the exact same name as the result? Commented May 11, 2016 at 11:17
  • yes I just copy and pass to DB and I try to initial to $Model_name value like this too $ModelName = "ClassName". Notes: ClassName is $table->tables value in DB Commented May 11, 2016 at 11:22

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

I have to initial $modelName = 'MyModel' and then I will used as $loan = $modelName::select('*')->where('id','=', $id)->get(); but it still the same errors types. as you said to used string to create Objects of class and call it as static I dont understand could you make a demo. I've tried with PHP method to clean that string or without using but still can't. but as How do you think as below answer?
I added sample code to support the statements I put in the answer. I cannot tell if this is your problem, I have a hunch that in your code the problem is somewhere else.

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.