From the course: PHP: Design Patterns

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Solution: Apply the publish/subscriber pattern

Solution: Apply the publish/subscriber pattern - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Solution: Apply the publish/subscriber pattern

- This one took a little more. Unlike our after code from before, we couldn't just call the methods and be done with it. As our first step we needed a Dispatcher. Which we could just copy from the after code if we wanted to. Once we have a Dispatcher we needed to register all of our listeners in index.php, which we did here. All that should look pretty familiar. Of course, if we stop there, we end up with our count being off. You should see the count be the number two twice. Definitely not what we want. This is because when C is triggered off of A the value from B triggering has not been passed along to C. So this is what we need to modify. This is where things get different in that our input value is supposed to change over time. Therefore, our addOneAndEcho needs to return the result it just generated. So you can return that here. And another difference is the Dispatcher needs to catch that result here and feed it back into the method next time. Notice that the publish method now…

Contents