{"id":8354,"date":"2025-09-02T06:33:53","date_gmt":"2025-09-02T06:33:53","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=8354"},"modified":"2025-09-02T06:40:36","modified_gmt":"2025-09-02T06:40:36","slug":"understanding-the-observer-pattern-with-an-example-in-php","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/","title":{"rendered":"Understanding the Observer Pattern with an Example in PHP"},"content":{"rendered":"<p>The Observer pattern in <a href=\"https:\/\/studysection.com\/blog\/adapter-pattern-with-example-in-php\/\">PHP<\/a> is a behavioral design pattern that allows an object, known as the subject, to maintain a list of dependents, called observers, and notify them of any state changes, usually by calling one of their methods. This <a href=\"https:\/\/blog.webnersolutions.com\/regular-expressions-in-php\/\">pattern<\/a> is useful when you need a way to update multiple objects when the state of another object changes, without tightly coupling the objects together.<\/p>\n<p><strong>Key Concepts:<\/strong><\/p>\n<ul>\n<li><strong>Subject:<\/strong> The object that tracks its state and informs observers about changes.<\/li>\n<li><strong>Observer:<\/strong> The object that needs to be notified about changes in the subject.<\/li>\n<\/ul>\n<p>Let\u2019s consider a scenario where we have a blog, and we want to notify subscribers whenever a new post is published. Here, the blog is the subject, and the subscribers are the observers. We are explaining their implementation step by step here:<\/p>\n<p>1. First we create an Observer Interface in which we define the \u201dupdate\u201d method that needs to be implemented by all concrete observers.<\/p>\n<pre><code>&lt;?php\r\ninterface Observer {\r\n   public function update($postTitle);\r\n}<\/code><\/pre>\n<p>2. Then we create a \u201cSubscriber\u201d class that implements the \u201dupdate\u201d method to define what happens when the subscriber is notified about a new post.<\/p>\n<pre><code>&lt;?php\r\nclass Subscriber implements Observer {\r\n   private $name;\r\n   public function __construct($name) {\r\n       $this-&gt;name = $name;\r\n   }\r\n   public function update($postTitle) {\r\n       echo $this-&gt;name . \" has been notified about a new post: \" . $postTitle . \"\";\r\n   }\r\n}<\/code><\/pre>\n<p>3. After that, we will create \u201cSubject\u201d Interface which defines methods to attach, detach, and notify observers.<\/p>\n<pre><code>&lt;?php\r\ninterface Subject {\r\n   public function attach(Observer $observer);\r\n   public function detach(Observer $observer);\r\n   public function notify();\r\n}<\/code><\/pre>\n<p>4. Now, create a subject class \u201cBlog\u201d that implements the \u201cSubject\u201d interface and maintains a list of subscribers. When a new post is added using the `addPost` method, it notifies all subscribers.<\/p>\n<pre><code>&lt;?php \r\nclass Blog implements Subject { \r\nprivate $subscribers = []; \r\nprivate $latestPost; \r\n\r\npublic function attach(Observer $observer) { \r\n$this-&gt;subscribers[] = $observer;\r\n   }\r\n\r\n   public function detach(Observer $observer) {\r\n       $index = array_search($observer, $this-&gt;subscribers);\r\n       if ($index !== false) {\r\n           unset($this-&gt;subscribers[$index]);\r\n       }\r\n   }\r\n\r\n   public function notify() {\r\n       foreach ($this-&gt;subscribers as $subscriber) {\r\n           $subscriber-&gt;update($this-&gt;latestPost);\r\n       }\r\n   }\r\n\r\n   public function addPost($postTitle) {\r\n       $this-&gt;latestPost = $postTitle;\r\n       $this-&gt;notify();\r\n   }\r\n}<\/code><\/pre>\n<p>5. Using the below code, we are creating subscribers, attaching them to the blog, and adding a new post to trigger notifications.<\/p>\n<pre><code>&lt;?php \r\n$subscriber1 = new Subscriber(\"Alice\"); \r\n$subscriber2 = new Subscriber(\"Bob\"); \r\n$subscriber3 = new Subscriber(\"Charlie\"); \r\n\r\n\/\/ Creating subject (blog) \r\n$blog = new Blog(); \r\n\r\n\/\/ Attaching subscribers to the blog \r\n$blog-&gt;attach($subscriber1);\r\n$blog-&gt;attach($subscriber2);\r\n$blog-&gt;attach($subscriber3);\r\n\r\n\/\/ Adding a new post, which triggers notifications\r\n$blog-&gt;addPost(\"Understanding the Observer Pattern in PHP\");<\/code><\/pre>\n<p>As demonstrated in the example above, subscribers can be added or removed without altering the blog class. This promotes loose coupling, making the system more flexible and easier to maintain. When a new post is published, all subscribers are automatically notified, ensuring they remain up-to-date with the latest content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Observer pattern in PHP is a behavioral design pattern that allows an object, known as the subject, to maintain<\/p>\n","protected":false},"author":1,"featured_media":8355,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Observer Pattern with an Example in PHP.<\/title>\n<meta name=\"description\" content=\"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Observer Pattern with an Example in PHP.\" \/>\n<meta property=\"og:description\" content=\"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2025-09-02T06:33:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-09-02T06:40:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/09\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-8.png\" \/>\n\t<meta property=\"og:image:width\" content=\"940\" \/>\n\t<meta property=\"og:image:height\" content=\"788\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Understanding the Observer Pattern with an Example in PHP\",\"datePublished\":\"2025-09-02T06:33:53+00:00\",\"dateModified\":\"2025-09-02T06:40:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\"},\"wordCount\":308,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\",\"url\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\",\"name\":\"Observer Pattern with an Example in PHP.\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2025-09-02T06:33:53+00:00\",\"dateModified\":\"2025-09-02T06:40:36+00:00\",\"description\":\"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding the Observer Pattern with an Example in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Observer Pattern with an Example in PHP.","description":"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/","og_locale":"en_US","og_type":"article","og_title":"Observer Pattern with an Example in PHP.","og_description":"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.","og_url":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2025-09-02T06:33:53+00:00","article_modified_time":"2025-09-02T06:40:36+00:00","og_image":[{"width":940,"height":788,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2025\/09\/Orange-and-Beige-Simple-Icon-Beauty-and-Fashion-Logo-8.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Understanding the Observer Pattern with an Example in PHP","datePublished":"2025-09-02T06:33:53+00:00","dateModified":"2025-09-02T06:40:36+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/"},"wordCount":308,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/","url":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/","name":"Observer Pattern with an Example in PHP.","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2025-09-02T06:33:53+00:00","dateModified":"2025-09-02T06:40:36+00:00","description":"The Observer pattern is a behavioral design pattern that allows an object to maintain a list of dependents, called observers, and notify them of any state changes.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/understanding-the-observer-pattern-with-an-example-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Understanding the Observer Pattern with an Example in PHP"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":50,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8354"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=8354"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8354\/revisions"}],"predecessor-version":[{"id":8358,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/8354\/revisions\/8358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/8355"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=8354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=8354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=8354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}