Jump to content

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.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.