Dynamically generate classes at runtime in php?

后端 未结 10 1053
一向
一向 2020-12-09 15:06

Here\'s what I want to do:

$clsName = substr(md5(rand()),0,10); //generate a random name
$cls = new $clsName(); //create a new instance

function __autoload(         


        
10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 15:19

    We can create class instance dynamically by following way

    I also face this issue in Laravel 5.8 version and now it is working fine for me.

    Give full path instead of the class Name

    class TestController extends Controller
    {
        protected $className;
    
        public function __construct()
        {
            $this->className = 'User';
        }
    
        public function makeDynamicInstance()
        {
            $classNameWithPath = 'App\\' . $this->className; 
            $classInstance = new $classNameWithPath;
            $data = $classInstance::select('id','email')->get();
            return $data;
        }
    }
    

    Output

    Illuminate\Database\Eloquent\Collection Object
    (
        [items:protected] => Array
            (
                [0] => App\User Object
                    (
                        [fillable:protected] => Array
                            (
                                [0] => name
                                [1] => email
                                [2] => password
                                [3] => user_group_id
                                [4] => username
                                [5] => facebook_page_id
                                [6] => first_name
                                [7] => last_name
                                [8] => email_verified
                                [9] => active
                                [10] => mobile
                                [11] => user_type
                                [12] => alternate_password
                                [13] => salt
                                [14] => email_verification_token
                                [15] => parent_id
                            )
    
                        [hidden:protected] => Array
                            (
                                [0] => password
                                [1] => remember_token
                            )
    
                        [casts:protected] => Array
                            (
                                [email_verified_at] => datetime
                            )
    
                        [connection:protected] => mysql
                        [table:protected] => users
                        [primaryKey:protected] => id
                        [keyType:protected] => int
                        [incrementing] => 1
                        [with:protected] => Array
                            (
                            )
    
                        [withCount:protected] => Array
                            (
                            )
    
                        [perPage:protected] => 15
                        [exists] => 1
                        [wasRecentlyCreated] => 
                        [attributes:protected] => Array
                            (
                                [id] => 1
                                [email] => admin@admin.com
                            )
    
                        [original:protected] => Array
                            (
                                [id] => 1
                                [email] => admin@admin.com
                            )
    
                        [changes:protected] => Array
                            (
                            )
    
                        [dates:protected] => Array
                            (
                            )
    
                        [dateFormat:protected] => 
                        [appends:protected] => Array
                            (
                            )
    
                        [dispatchesEvents:protected] => Array
                            (
                            )
    
                        [observables:protected] => Array
                            (
                            )
    
                        [relations:protected] => Array
                            (
                            )
    
                        [touches:protected] => Array
                            (
                            )
    
                        [timestamps] => 1
                        [visible:protected] => Array
                            (
                            )
    
                        [guarded:protected] => Array
                            (
                                [0] => *
                            )
    
                        [rememberTokenName:protected] => remember_token
                    )
    )
    

提交回复
热议问题