0

I found something about this error but I think this is little bit different. I defined a public variable.

Class Controller{    
    public $model;

And I'm trying add extra word(model) between $model_name and $this.

public function call_model($model_name){
$this->model->$model_name = new $model_class;

What is the solution?

EDIT:

Warning: Creating default object from empty value in C:\xampp\htdocs\alisveris\project_library\Controller.php on line 16
5
  • What? If you want to have a property $model that has properties itself, it needs to be a object itself Commented Dec 22, 2013 at 14:16
  • Then need i hide the errors? or delete the 'model'? Commented Dec 22, 2013 at 14:18
  • Your problem is not clear. Please provide more details. Commented Dec 22, 2013 at 14:19
  • @chanchal118 I have added warning.. Commented Dec 22, 2013 at 14:28
  • what does the $model_class variable contains? It seems to me that it is empty, and therefore you are getting that error. Commented Dec 22, 2013 at 14:34

2 Answers 2

1

You can create static factory method to create different models like this:

abstract class Model {
 static function CallModel($name) {
  switch ( $name ) {
   case 'Naomi': return new NaomiCampbell();
   case 'Anja': return new AnjaRubik();
   default: return new $name;
  }
 }
}

class NaomiCampell extends Model {}
class AnjaRubik extends Model {}

Then use:

$MyModel = Model::CallModel($name);
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it by assigning $model variable to $this

 class Db
  {
         public function great()
         {
             echo 'great';
         }
  }


 class Controller{

     public $model;

     public function __construct()
     {
         $this->model = $this;

         $model_name = 'Db';
         $this->model->$model_name = new $model_name;

     }
 }

 $cc = new Controller();

 echo $cc->model->Db->great();

Output

great

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.