php从空值创建默认对象?

发布于 2024-10-15 05:49:57 字数 975 浏览 2 评论 0原文

好吧,我想做的是做一些东西,这样我就可以这样称呼它 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青衫负雪 2024-10-22 05:49:57

仅凭该代码很难看出您实际上做错了什么。我编写了一些非常简单的代码来重现该错误:

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

$foo->bar = $bar;

它给出此警告的原因是您正在以“对象方式”分配值,但您将其分配给不是对象的变量。通过这样做,Zend 引擎实际上为 $foo 创建了一个对象,它是 StdClass 的一个实例。显然,十分之九,这不是您想要做的,因此 PHP 提供了一条有用的消息。

在你的情况下: $this->model 还不是一个对象。如果您想消除该错误,只需执行以下操作:

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

干杯。

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:

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

$foo->bar = $bar;

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:

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

Cheers.

美羊羊 2024-10-22 05:49:57

您必须使用 __get 魔术方法 - http://php .net/manual/pl/language.oop5.magic.php

您可以通过执行类似的操作来实现您正在寻找的目标:

<?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

但是您应该避免像上面那样的魔法 ->更好的方法是这样做:

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

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

You can achieve what you're looking for doing something like that:

<?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 like above -> better approach is to do it that way:

//model creator is an instance of model creator
$this->modelCreator->getModel('user'); // now you know what exactly is happening
淡看悲欢离合 2024-10-22 05:49:57

必须使用双 $

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

Must use double $'s

$this->model->$model = new $class;
尾戒 2024-10-22 05:49:57

除了 Berry Langerak 的回答之外,

is_object 仍会触发严格检查,因为它假设 $this->model 中存在“某些内容”。 isset是一个更好的方法

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

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

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

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

$this->model->$model = new $class;
青春如此纠结 2024-10-22 05:49:57
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();
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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文