在类中使用stdClass作为php 5+中的类var

时间:2011-11-21 22:53:45

标签: php

我似乎无法做到这一点..当我尝试添加应该是范围的内容时,我似乎一直在创建一个新的stdClass

    class d_elem {
        private $el;
        function __constructor($p) {
            $el = new stdClass;
        }
        private static function ele_base($p) {
            print('ele_base<br/>');
            d_elem::ele_base_attr($p);
        }
        private static function ele_base_attr($p) {
            print('ele_base_attr<br/>');
            isset($p['id']) ? $el->id = '' . $p['id'] . '' : '';
            print_r($p);print('<br/>');
            print_r($el);print('<br/>'); //<< should have added the id but created a new one????
        }
        public static function ele_a($p) {
            d_elem::ele_base($p);
            isset($p['href']) ? $el->href = '' . $p['href'] . '' : '';
            isset($p['TEXT']) ? $el->TEXT = '' . $p['TEXT'] . '' : '';
            print_r($p);print('<br/>');
            print_r($el);print('<br/>');//<< should have added the id but only has the href and TEXT when all 3 should be there
            //skip the retrun
        }
    }

    echo d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test'));

有人有想法吗?

谢谢你-Jeremy

[编辑] -------------------------------------- 根据上面的建议

    class d_elem{
        public static $el;
        private static function init(){ 
            self::$el = new stdClass; 
        } 
        private static function ele_base($p) {
            print('ele_base<br/>');
            self::ele_base_attr($p);
        }
        private static function ele_base_attr($p) {
            print('ele_base_attr<br/>');
            isset($p['id']) ? $el->id = '' . $p['id'] . '' : '';
            print_r($el);print('<br/>'); //<< should have added the id but created a new one????
        }
        public static function ele_a($p) {
            self::init();
            self::ele_base($p);
            isset($p['href']) ? $el->href = '' . $p['href'] . '' : '';
            isset($p['TEXT']) ? $el->TEXT = '' . $p['TEXT'] . '' : '';
            print_r($el);print('<br/>');
            //skip the retrun
        }
    }

    d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test'));

仍然产生相同的

输出

ele_base

ele_base_attr

stdClass对象([id] =&gt; test)

stdClass对象([href] =&gt;#[TEXT] =&gt;测试)

并希望

stdClass对象([id] =&gt; test [href] =&gt;#[TEXT] =&gt; test)

tk -J

[终止解决方案]

        class d_elem {
            private static $el;  /// fill this as we run from function to function
            private static function init(){ // start off but creating the object
                self::$el = new stdClass; 
            } 
            private static function ele_base($p) {
                self::ele_base_attr($p);// here we fill base on some condition.. simple test first
            }
            private static function ele_base_attr($p) {
                isset($p['id']) ? self::$el->id = ' id="' . $p['id'] . '" ' : '';  // this should be pushed to the class level object
            }
            public static function ele_a($p) {
                $p=!is_array($p)?get_object_vars ($p):$p; // make sure that if p is an object we trun it to an array
                self::init();// set the class object
                self::ele_base($p);// make first add to class object
                isset($p['href']) ? self::$el->href = ' href="' . $p['href'] . '" ' : ''; make second add to the class object
                foreach (self::$el as $key => $value) {
                    $ele .= $value; // spit the values back out to return from the class object
                }
                $ele .= $p['TEXT'] ; // test something to return at this level 
                return $ele // return all the properties in this case a string of them
            }
        }

        echo d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test')); // call for the output

3 个答案:

答案 0 :(得分:1)

我无法解决所有问题,但有几点需要注意:

永远不会调用

__constructor(),因为类永远不会被实例化,只能静态调用(并且应该被称为__construct())。

没有必要echo d_elem::elem_a()因为没有返回任何内容。

如果要将该函数用作静态函数,$el变量也需要是静态的,并称为self::$el。类似地,在对象内部调用静态函数时,应将其称为self::ele_base_attr()等。


编辑:您可以将__construct转换为静态函数,即:

private static function init()
{
    self::$el = new stdClass;
}

然后在self::init()中执行任何其他操作之前调用d_elem::elem_a()


编辑:这实现了同样的目的:

class d_elem {
    public static $el;

    public static function ele_a($p) {
        self::$e = new stdClass;

        if(isset($p['href'])) $el->href = $p['href'];
        if(isset($p['TEXT'])) $el->TEXT = $p['TEXT'];
        if(isset($p['id'])) $el->id = $p['id'];

        print_r(self::$el);
    }
}

d_elem::ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test'));

答案 1 :(得分:0)

请参阅$thisself,以及late static bindings

答案 2 :(得分:0)

你试过这个吗?:

isset($p['id']) ? $this->el->id = '' . $p['id'] . '' : '';

这个?:

print_r($this->el);print('<br/>');

在一个类中,为了从同一个类中的方法/函数中访问声明的变量(对于整个类使用,如$el),您需要进行更改$el(或任何相同范围的变量)到$this->el