0

I can't seem to get this right.. It seems I keep creating a new stdClass when I'm tryng to add to what should be one for the scope.

    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'));

Any one have an idea?

thank you -Jeremy

[EDIT]-------------------------------------- per the suggestion above

    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'));

Still produces the same output of

ele_base

ele_base_attr

stdClass Object ( [id] => test )

stdClass Object ( [href] => # [TEXT] => test )

.

and want

stdClass Object ( [id] => test [href] => # [TEXT] => test )

tk -J

[END SOLUTION AS OF YET]

        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
5
  • And what are you trying to achieve? Commented Nov 21, 2011 at 22:59
  • $el = new stdClass; should be $el = new stdClass(); Commented Nov 21, 2011 at 22:59
  • @RafeKettler it doesn't matter in PHP at all Commented Nov 21, 2011 at 23:01
  • @Ondrej Slinták I want to keep adding to $el as i move thru the functions. I don't want to pass it thro as I'm looking for a simple way to hold the info. Hope the makes sense here I did comment the code to help better I hope. tk -J Commented Nov 21, 2011 at 23:06
  • @RafeKettler ; Strangely, $el = new stdClass; works the same way that $el = new stdClass(); does.. but I suppose the latter is the better practice? Commented Nov 21, 2011 at 23:06

3 Answers 3

1

I can't fix everything, but a few things to note:

__constructor() never gets called, because the class never gets instantiated, only called statically (and should be called __construct()).

There's no need to echo d_elem::elem_a() because nothing is returned.

If you want to use the function as static functions, the $el variable also needs to be static, and referred to as self::$el. Similarly, when calling static functions inside the object you should call them as self::ele_base_attr() etc.


EDIT: You could turn the __construct into a static function, ie:

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

Then call self::init() before doing anything else in d_elem::elem_a().


EDIT: This achieves the same thing:

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'));
Sign up to request clarification or add additional context in comments.

9 Comments

well few things on this.. echo d_elem::elem_a() was just to call it in to action.. echo is the not needed part :D .. self:: and su_elements:: worked the same.. can't beleive I missed the typo but oldly changeing it to public function __construct(){ print('test<br/>'); $el = new stdClass(); } didn't help .. not sure.. didn't even get the print..
But as I said, __construct() is never called, because the class is never instantiated.
ok.. so doing what I really really don't want to do instantiate $Ele=new d_elem(); echo $Ele->ele_a(array('href'=>"#",'TEXT'=>'test','id'=>'test')); still doesn't solve the issues.. I really don't want to have to put $Ele=new d_elem(); everywhere too.. has to be a simple way to do this lol. tk -J
ok adding a answer as it's to long.. per what I think you suggested.
You still need to change the references to $el to self::$el within your functions.
|
0

See $this and self, also late static bindings

1 Comment

I don't see how this helps.. you can't do $this->el->id as this is a using $this when not in object context so that would be a fatal error.
0

Have you tried this?:

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

And this?:

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

Within a class, for you to access variables declared (for the entire class to use like $el) from within methods/functions within that same class, you'll need the change $el (or any variable of the same scope) to $this->el

1 Comment

as per the other comment I left "you can't do $this->el->id as this is a using $this when not in object context so that would be a fatal error" so needs to be something else.. tk -J

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.