1

这是我的代码:

<?php

class VortexORM {

    private static $orm = null;

    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public static function getInstance() {
        if (VortexORM::$orm == null)
            VortexORM::$orm = new VortexORM();
        return VortexORM::$orm;
    }

    public static function set($name, $value) {
        $orm = VortexORM::getInstance();
        //echo "Setting [ <b>{$name}</b> ::   <i>{$value}</i>]";
        $orm->$name = $value;
    }

    public static function get($name) {
        $orm = VortexORM::getInstance();
        // echo "Getting [ <b>{$name}</b> ::   <i>{$orm->$name}</i>]";
        return $orm->$name;
    }

}

要获取我使用的数据:

var_dump(VortexORM::get('admin_links'));
var_dump(VortexORM::get('admin'));

要设置我使用的数据:

VortexORM::set('admin_links',array(....));

但是,我收到以下警告:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin_links
Filename: Vortex/VortexORM.php
Line Number: 8
NULL
A PHP Error was encountered
Severity: Notice
Message: Undefined property: VortexORM::$admin
Filename: Vortex/VortexORM.php
Line Number: 8

为什么我会收到这些警告?

我希望能够在 CodeIgniter 中像这样作为静态函数访问它:

$this->vortexorm->admin_links = array(....);
4

1 回答 1

-1

好的,我刚刚测试了这段代码:

    <?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class VortexORM {

    private static $orm = null;

    public function __get($name) {
        return $this->$name;
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public static function getInstance() {
        if (VortexORM::$orm == null)
            VortexORM::$orm = new VortexORM();
        return VortexORM::$orm;
    }

    public static function set($name, $value) {
        $orm = VortexORM::getInstance();
        //echo "Setting [ <b>{$name}</b> ::   <i>{$value}</i>]";
        $orm->$name = $value;
    }

    public static function get($name) {
        $orm = VortexORM::getInstance();
        // echo "Getting [ <b>{$name}</b> ::   <i>{$orm->$name}</i>]";
        return $orm->$name;
    }

}

VortexORM::set('admin_links',"test");

var_dump(VortexORM::get('admin_links'));

它对我来说很好用。给出以下输出:

string(4) "test" 

所以,我想,你可能只是想在设置之前检索一个属性?或者请分享更多细节。另外,您为什么要尝试创建新的 ORM,我们可以在其中轻松地使用带有 codeigniter 的学说

于 2013-02-16T07:51:20.453 回答