Jump to content

Static Method/Property doesn't append text


The Little Guy

Recommended Posts

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?

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?!?!

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.