The Little Guy Posted July 6, 2012 Share Posted July 6, 2012 Am I missing something? Whenever I try to append data to the end of a string from an array, it doesn't get put there. Class Template{ public static $items = array(); static public function append($key, $data){ if(!isset(self::$items[$key])){ self::$items[$key] = ""; } self::$items[$key] .= $data; } } Example: class ClassA{ public function __construct(){ Template::append("CONTENT", "This is ClassA"); } } class ClassB{ public function __construct(){ Template::append("CONTENT", "This is ClassB"); } } $classA = new ClassA(); $classB = new ClassB(); print_r(Template::$items); When I do a print_r, the only thing in $items is This is ClassA What isn't This is ClassB in there? Link to comment https://forums.phpfreaks.com/topic/265285-static-methodproperty-doesnt-append-text/ Share on other sites More sharing options...
kicken Posted July 6, 2012 Share Posted July 6, 2012 The posted code works fine for me, returning: Array ( [CONTENT] => This is ClassAThis is ClassB ) Make sure you didn't miss the . in your .= operator. That sounds like the most likely cause of what you're seeing. Link to comment https://forums.phpfreaks.com/topic/265285-static-methodproperty-doesnt-append-text/#findComment-1359522 Share on other sites More sharing options...
The Little Guy Posted July 6, 2012 Author Share Posted July 6, 2012 The posted code works fine for me, returning: Array ( [CONTENT] => This is ClassAThis is ClassB ) Make sure you didn't miss the . in your .= operator. That sounds like the most likely cause of what you're seeing. Yeah the . is in there, I copied and pasted the method directly from the code. There has got to be something else going on then... I don't even know where to start with debugging I have already gone through the page load process like 5 times, and I am not seeing it. When It is run on my site, the first classes data gets loaded into the variable but not any of the ones after it for some reason. I echoed some text right after I called Template::append() and they were both echoed out, so I know each function is being called. But why is the second one not getting put into the array?!?! Link to comment https://forums.phpfreaks.com/topic/265285-static-methodproperty-doesnt-append-text/#findComment-1359524 Share on other sites More sharing options...
The Little Guy Posted July 6, 2012 Author Share Posted July 6, 2012 Found it! I had a return that was exiting the execution of the second method in another unrelated method! foreach($this->listeners as $hook){ if(method_exists($this->$hook, $method)){ return call_user_func(array($this->$hook, $method)); } } Link to comment https://forums.phpfreaks.com/topic/265285-static-methodproperty-doesnt-append-text/#findComment-1359528 Share on other sites More sharing options...
xyph Posted July 6, 2012 Share Posted July 6, 2012 This is why global/static values are generally considered bad (yes, they definitely have their place though). It can get incredibly hard to debug Link to comment https://forums.phpfreaks.com/topic/265285-static-methodproperty-doesnt-append-text/#findComment-1359534 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.