{"id":1613,"date":"2017-03-22T13:33:09","date_gmt":"2017-03-22T08:03:09","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1613"},"modified":"2017-03-29T09:57:18","modified_gmt":"2017-03-29T04:27:18","slug":"new-self-vs-new-static","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/new-self-vs-new-static\/","title":{"rendered":"PHP &#8211; New self vs. new static"},"content":{"rendered":"<h4 id=\"new-self\"><span style=\"color: #ff6600;\"><strong>new self:<\/strong><\/span><\/h4>\n<ul>\n<li>self points to the class in which it is written.<\/li>\n<li>self refers to the same class in which the new keyword is actually written.<\/li>\n<\/ul>\n<h4 id=\"new-static\"><span style=\"color: #800000;\"><strong>New static<\/strong><\/span><\/h4>\n<ul>\n<li>static, in PHP 5.3\u2019s late static bindings, refers to whatever class in the hierarchy you called the method on.<\/li>\n<\/ul>\n<h4 id=\"what-is-the-difference-between-self-and-static\"><span style=\"color: #000000;\"><b>what is the difference between self and static?<\/b><\/span><\/h4>\n<ul>\n<li>The differences between self and static are fairly easy to understand with some good examples.<\/li>\n<li>So, let\u2019s take a look at some actual code. Suppose we have the following class \u2013 called Car \u2013 which has two simple methods called model and getModel. Note the use of the self keyword:<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><b>Example of self in PHP:<\/b><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class Car<br\/>{<br\/>    public static function model()<br\/>{<br\/>         self::getModel();<br\/>}<br\/><br\/>    protected static function getModel()<br\/>{<br\/>        echo &quot;I am a Car!&quot;;<br\/>}<br\/><br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>Suppose we make this call to the static model function in the Car class \u2013 since it is a static function we can of course call the function directly using only the class name:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Car::model();<\/code><\/pre> <\/div>\n<p>The output after calling the model function in the Car class will be:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">I am a Car!<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>The self keyword simply makes a call to the getModel function that belongs to the Car class, which just outputs the text \u201cI am a Car!\u201d to the page.<\/li>\n<li>Let\u2019s say that we decide to add a new class, called Mercedes, that derives from the Car class.<\/li>\n<\/ul>\n<p><span style=\"color: #000000;\"><strong>Here is what it looks like:<\/strong><\/span><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class Mercedes extends Car<br\/>{<br\/><br\/>    protected static function getModel()<br\/> {<br\/>        echo &quot;I am a Mercedes!&quot;;<br\/>    }<br\/><br\/>}<\/code><\/pre> <\/div>\n<ul>\n<li>Because the Mercedes class derives from the Car class, it will inherit the model function that is defined in the Car class.<\/li>\n<\/ul>\n<p>So, what do you think will happen if we call the static model function, but this time we use the Mercedes class instead \u2013 like this:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Mercedes::model();<\/code><\/pre> <\/div>\n<p>Well, the output from the function call above may not be what you expect \u2013 this is what the output looks like:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">I am a Car!<\/code><\/pre> <\/div>\n<ul>\n<li>You may have been expecting the Mercedes::model(); call to have output \u201cI am a Mercedes!\u201d. So, what is going on here?<\/li>\n<\/ul>\n<h4 id=\"explaining-self\"><span style=\"color: #008000;\"><b>Explaining self:<\/b><\/span><\/h4>\n<ul>\n<li>The model function is defined inside the Car class, and it is not overridden by the Mercedes class \u2013 but the model function is of course inherited by the Mercedes class.<\/li>\n<li>As a result, when we call the version of model inside the Mercedes class, the scope of the function is still inside the Car class \u2013 because the function definition is inside the Car class.<\/li>\n<li>The way the keyword \u201cself\u201d works is that it will call the current class\u2019s implementation of the getModel function \u2013 and since the model function is defined inside the Car class, the current class would be the Car class.<\/li>\n<li>So, it will call the Car class implementation of getModel and NOT the Mercedes class implementation<\/li>\n<li>This behavior may be considered undesirable because it is not polymorphic, and is not aligned with object oriented design principles.<\/li>\n<li>But, there is an alternative solution that can get us that kind of behavior \u2013 and this is where the static keyword becomes useful.<\/li>\n<li>This behavior may be considered undesirable because it is not polymorphic, and is not aligned with object oriented design principles.<\/li>\n<li>But, there is an alternative solution that can get us that kind of behavior \u2013 and it involves using the static keyword in a different way from how you may normally use it.<\/li>\n<\/ul>\n<h4 id=\"late-static-or-new-static-binding\"><span style=\"color: #000000;\"><b>late static or new static binding:<\/b><\/span><\/h4>\n<ul>\n<li>In PHP 5.3, a new feature called late static bindings was added \u2013 and this can help us get the polymorphic behavior that may be preferable in this situation.<\/li>\n<li>In simplest terms, late static bindings means that a call to a static function that is inherited will \u201cbind\u201d to the calling class at runtime.<\/li>\n<li>\u00a0So, in our example above, if we use late static binding it would mean that when we make a call to \u201cMercedes::model();\u201d, then the getModel function in the Mercedes class will be called instead of the getModel function in the Car class.<\/li>\n<li>Mercedes is of course the calling class in our example.<\/li>\n<\/ul>\n<h4 id=\"example-of-late-static-binding-in-php\"><span style=\"color: #000000;\"><b>Example of late static binding in PHP<\/b><\/span><\/h4>\n<ul>\n<li>Now, the question is how can we actually make late static binding work for us? Well, all we have to do is replace the \u201cself::getModel();\u201d call inside the Car class with \u201cstatic::getModel();\u201d instead.<\/li>\n<li>So, this is what our new Car class will look like \u2013 note that we do not have to make any change to the Mercedes class:<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class Car<br\/>{<br\/>    public static function model()<br\/>    {<br\/>         static::getModel();<br\/>    }<br\/><br\/>    protected static function getModel()<br\/>    {<br\/>        echo &quot;I am a Car!&quot;;<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Now, if we make this call:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">Mercedes::model();<\/code><\/pre> <\/div>\n<p><strong>Our output will be this:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">php code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">              I am a Mercedes!<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"late-static-binding-was-not-possible-before-php-5-3\"><span style=\"color: #000000;\"><b>Late static binding was not possible before PHP 5.3:<\/b><\/span><\/h4>\n<ul>\n<li>Note that before PHP 5.3 late static binding was not possible \u2013 and trying to run the code above in any version of PHP before 5.3 will return an error.<\/li>\n<\/ul>\n<h4 id=\"php-self-versus-static\"><span style=\"color: #000000;\"><b>PHP self versus static:<\/b><\/span><\/h4>\n<ul>\n<li>Now that we changed the code in our example to use static instead of self, you can see the difference is that self references the current class,<\/li>\n<li>whereas the static keyword allows the function to bind to the calling class at runtime.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>new self: self points to the class in which it is written. self refers to the same class in which the new keyword is actually written. New static static, in PHP 5.3\u2019s late static bindings, refers to whatever class in the hierarchy you called the method on. what is the difference between self and static? [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[3331,3333,3332,3330,3336,3339,3327,3337,3338,3340,3341,404,3334,3329,3328,3335],"class_list":["post-1613","post","type-post","status-publish","format-standard","hentry","category-php","tag-caching-via-static-properties-in-php","tag-create-two-same-static-classes-in-php","tag-is-it-good-to-use-self-or-static-on-non-static-function","tag-is-there-a-middle-ground-between-self-and-static","tag-late-static-bindings","tag-new-static-c","tag-php-5-2-equivalent-to-late-static-binding-new-static","tag-php-new-static-constructor-php-static-vs-self-performance","tag-php-new-static-object","tag-php-self-static","tag-php-static-keyword","tag-reference-what-does-this-symbol-mean-in-php","tag-staticproperty-in-trait-returns-data-bound-to-the-trait-instead-of-class","tag-undefined-class-constant-selfstring","tag-why-doesnt-late-static-binding-work-with-variables-in-php-5-3","tag-why-return-new-static-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1613"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1613\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}