Php Create a default object from an empty value?

OK, what I'm trying to do is do something so that I can call it $this->model->users->getInfomation('name'); or something similar in my structure but php gives me strict standards Creating a default object from an empty value

 protected function model($model) { $path = "features". DS ."models". DS . $model .".php"; require $path; $class = 'Model'. ucfirst($model); $this->model->$model = new $class; } 

can we do it so that it somehow complies with the standards?

change *

this function is in the Application class, so I can extend them from our controller as a blog Extends the application and then calls something like $ this-> model-> blog will get something like what I do above when I do something like

 protected function model($model) { $path = "features". DS ."models". DS . $model .".php"; require $path; $class = 'Model'. ucfirst($model); $this->$model = new $class; } 

yes, the code above works fine $this->blog->getSomething(); , but for some reason I want to make them in a group, for example, above, so if we want to get something like $this->model->blog->getSomething();

Thanks for the time.

Adam Ramadhan

+7
source share
5 answers

It's hard to understand what you are actually doing wrong with this code. I made very simple code to reproduce the error:

 <?php $bar = 42; $foo = null; $foo->bar = $bar; 

The reason this warning is because you assign the values ​​"object path", but you assign it to a variable that is not an object. By doing this, the Zend engine actually creates an object for $ foo, which is an instance of StdClass. Obviously, 9 out of 10 times is not what you want to do, so PHP provides a useful post.

In your case: $ this-> model is not an object (yet). If you want to get rid of the error, just do:

 if( !is_object( $this->model ) ) { $this->model = new StdClass; } $this->model->$model = new $class; 

Greetings.

+7
source

You should use the __get magic method - http://php.net/manual/pl/language.oop5.magic.php

You can achieve what you are looking to do something like this:

 <?php class ModelCreator { private $_modelsCreated = array(); public function __get($model) { $class = 'Model'. ucfirst($model); //avoid creating multiple same models if (!array_key_exists($model, $this->_modelsCreated)) { $path = "features". DS ."models". DS . $model .".php"; require_once 'modeluser.php'; $this->_modelsCreated[$class] = new $class; } return $this->_modelsCreated[$class]; } } class MyClass { private $_model; public function __construct(ModelCreator $model) { $this->_model = $model; } public function __get($name) { if ($name === 'model') { return $this->_model; } } } $myClass = new MyClass(new ModelCreator()); $userModel = $myClass->model->user; // will return a class of ModelUser 

But you should avoid magic as shown above -> the best approach is to do it like this:

 //model creator is an instance of model creator $this->modelCreator->getModel('user'); // now you know what exactly is happening 
+2
source

Must use double $

 $this->model->$$model = new $class; 
0
source

In addition to Berry Langerac, answer

is_object still calls for rigorous validation , as it assumes there is something in the $ this-> model. isset is the best approach

 if( !isset( $this->model ) ) { $this->model = new StdClass; } $this->model->$model = new $class; 
0
source
 if( !is_object( $this->request ) ) { $this->request = new StdClass; } if(!property_exists($this->request, 'method')) { $this->request->method = new StdClass; } // How is this request being made? POST, DELETE, GET, PUT? $this->request->method = $this->_detect_method(); 
0
source

All Articles