php从空值创建默认对象?
好吧,我想做的是做一些东西,这样我就可以这样称呼它 $this->model->users->getInfomation('name');
或我的框架上类似的东西 但是 php 给了我一个严格的标准从空值创建默认对象
protected function model($model)
{
$path = "features". DS ."models". DS . $model .".php";
require $path;
$class = 'Model'. ucfirst($model);
$this->model->$model = new $class;
}
我们可以让它以某种方式符合标准吗?
编辑*
此函数位于应用程序类中,因此我可以从我们的控制器扩展它们 像 blog Extends Application 然后调用类似 $this->model->blog 的东西会得到类似我上面所做的事情,当我做类似
protected function model($model)
{
$path = "features". DS ."models". DS . $model .".php";
require $path;
$class = 'Model'. ucfirst($model);
$this->$model = new $class;
}
是的,上面的代码工作正常 $this ->blog->getSomething();
,但不知何故我想将它们放在一个组中,就像上面的问题一样,所以如果我们想要得到类似 $this->model- 的东西>blog->getSomething();
感谢您抽出时间。
亚当·拉马丹
ok what im trying to do is makeing something so i can call it like$this->model->users->getInfomation('name');
or something similer on my framework
but php give me a strict standards Creating default object from 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 make it so it will somehow fit in the standards ?
edit*
this function is in class Application so i can extend them from our controller
like blog Extends Application then call something like $this->model->blog will get something like what im doing 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 above code works fine $this->blog->getSomething();
, but somehow i want to make them in a group, like the question above, so if we want to get something like $this->model->blog->getSomething();
Thanks for the time.
Adam Ramadhan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仅凭该代码很难看出您实际上做错了什么。我编写了一些非常简单的代码来重现该错误:
它给出此警告的原因是您正在以“对象方式”分配值,但您将其分配给不是对象的变量。通过这样做,Zend 引擎实际上为 $foo 创建了一个对象,它是 StdClass 的一个实例。显然,十分之九,这不是您想要做的,因此 PHP 提供了一条有用的消息。
在你的情况下: $this->model 还不是一个对象。如果您想消除该错误,只需执行以下操作:
干杯。
It's hard to see what you're actually doing wrong with that code alone. I've made some very simple code to reproduce the error:
The reason it gives this warning, is that you're assigning values the "object way", but you're assigning it to a variable that isn't 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, this isn't what you want to do, so PHP provides a helpful message.
In your case: $this->model isn't an object (yet). If you want to get rid of the error, just do:
Cheers.
您必须使用
__get
魔术方法 - http://php .net/manual/pl/language.oop5.magic.php您可以通过执行类似的操作来实现您正在寻找的目标:
但是您应该避免像上面那样的魔法 ->更好的方法是这样做:
You must use
__get
magic method - http://php.net/manual/pl/language.oop5.magic.phpYou can achieve what you're looking for doing something like that:
But you should avoid magic like above -> better approach is to do it that way:
必须使用双 $
Must use double $'s
除了 Berry Langerak 的回答之外,
is_object 仍会触发严格检查,因为它假设 $this->model 中存在“某些内容”。 isset是一个更好的方法
In addition to Berry Langerak's answer
is_object will still trigger the strict check since it assumes there is 'something' in $this->model. isset is a better approach