【问题标题】:Simple ORM Structure Codeigniter简单的 ORM 结构 Codeigniter
【发布时间】:2013-01-31 23:31:54
【问题描述】:

这是我的代码:

<?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(....);

【问题讨论】:

    标签: php codeigniter properties undefined


    【解决方案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,我们可以在其中轻松 use doctrine with codeigniter

    【讨论】:

      猜你喜欢
      • 2019-08-12
      • 2014-02-03
      • 2020-11-13
      • 2012-04-09
      • 2015-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      相关资源
      最近更新 更多