{"id":790,"date":"2012-06-21T10:50:44","date_gmt":"2012-06-21T10:50:44","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=790"},"modified":"2012-06-21T10:50:44","modified_gmt":"2012-06-21T10:50:44","slug":"differences-between-php4-and-php5","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/","title":{"rendered":"Differences between PHP4 and PHP5"},"content":{"rendered":"<p>This listing of the differences between PHP4 and PHP 5 is is probably something that is quite common, but it is always worth a review.<\/p>\n<p>PHP5 is a lot different than PHP4 with the main differences being in how it handles objects and classes.<\/p>\n<p>Here are 10 major differences between PHP4 and PHP5 that you need to know:<\/p>\n<p><strong>1. Unified Constructors and Destructors:<\/strong><\/p>\n<p>In PHP4, constructors had same name as the class name. This used to cause overhead because every time you changed the class name, you had to change all the occurrences of that name.<\/p>\n<p>In PHP5, you simply need to name your constructors as __construct(). (the word \u2018construct\u2019 prefixed by double underscores). Similarly you can name your destructors as __destruct(). (the word \u2018destruct\u2019 prefixed by double underscores.) In destructors, you can write code that will get executed when the object is destroyed.<\/p>\n<p><strong>2. Abstract Class:<\/strong><\/p>\n<p>PHP5 lets you declare a class as \u2018Abstract\u2019. (i.e. a class whose object cannot be created. You can only extend an abstract class) Also, a class must be defined as abstract if it contains any abstract methods. And those abstract methods must be defined within the class which extend that abstract class. You can include complete method definitions within the abstract methods of abstract class.<\/p>\n<p><strong>3. Final Keyword:<\/strong><\/p>\n<p>PHP5 allows you to declare a class or method as \u2018Final\u2019 now. You just need to use \u2018final\u2019 keyword that will indicate that the class cannot be inherited or the method cannot be overridden.<\/p>\n<p><strong>4. Exception Handling:<\/strong><\/p>\n<p>PHP5 has introduced \u2018exceptions\u2019. An exception is simply a kind of error and the \u2018exception error\u2019 can be handled in an exception object. By using an exception, one can gain more control over the simple trigger_error notices we were stuck with before.<\/p>\n<p>When you are about to perform something \u2018risky\u2019 in your code, you can surround your code with a \u2018try\u2026catch\u2019 block. First you surround your code in a \u2018try {\u2026\u2026.}\u2019 block, then if an exception is thrown, your following \u2018catch{\u2026\u2026}\u2019 block is there to intercept the error and handle it accordingly. You can write some PHP code in your \u2018catch\u2019 block which will get executed when an error occurs in the \u2018try\u2019 block. If there is no \u2018catch\u2019 block, a fatal error occurs.<\/p>\n<p><strong>5. E_STRICT Error Level:<\/strong><\/p>\n<p>PHP5 introduces new error level defined as \u2018E_STRICT\u2019 (value 2048). This error levels notifies you when you use depreciated PHP code. It is not included in E_ALL, if you wish to use this new level you must specify it explicitly.<\/p>\n<p><strong>6. Autoloading (the __autoload() function):<\/strong><\/p>\n<p>PHP5 introduces a special function called \u2018__autoload()\u2019 (the word \u2018autoload\u2019 prefixed by double underscores). This function allows you to avoid writing a long list of includes at the top of your script by defining them inside this function. So you can automatically load object files when PHP encounters a class that hasn\u2019t been defined yet.<\/p>\n<p>Example:<\/p>\n<p>function __autoload ($class_name) {<\/p>\n<p>include $class_name . &#8216;.php&#8217;;<\/p>\n<p>}<\/p>\n<p><strong>7. Visibility:<\/strong><\/p>\n<p>In PHP5, class methods and properties now have \u2018visibility\u2019. There are 3 levels of visibilities:<\/p>\n<p>Public: \u2018Public\u2019 is the most visible. Methods are accessible to everyone including objects outside the classes. And properties readable and writable by everyone including objects outside the classes.<br \/>\nPrivate: \u2018Private\u2019 makes class members only available to the class itself.<br \/>\nProtected: \u2018Protected\u2019 makes class members accessible to the class itself and any inherited class (subclass) as well as any parent classes.<\/p>\n<p>PHP4\u2032s method of declaring a variable as \u2018var\u2019 keyword is still supported in PHP5. The \u2018var\u2019 keyword is now a synonym for the \u2018public\u2019 keyword now.<\/p>\n<p><strong>8. Pass by Reference:<\/strong><\/p>\n<p>In PHP4, everything was passed by value, including objects. Whereas in PHP5, all objects are passed by reference. Take a look at this PHP4 code for example &#8211;<\/p>\n<p>$peter = new Person();<br \/>\n$peter-&gt;sex = \u2019male\u2019;<\/p>\n<p>$maria = $peter;<br \/>\n$maria-&gt;sex = \u2019female\u2019;<\/p>\n<p>echo $peter-&gt;sex; \/\/ This will output \u2018female\u2019<\/p>\n<p>As you can see in the code above, if you wanted to duplicate an object in PHP4, you simply copied it by assigning it to another variable (Pass by value). But now in PHP5 you must use the new \u2018clone\u2019 keyword. So the above PHP4 code, will now look like this in PHP5 &#8211;<\/p>\n<p>$peter = new Person();<br \/>\n$maria = new Person();<\/p>\n<p>$peter-&gt;sex = \u2019male\u2019;<\/p>\n<p>$maria = clone $peter;<br \/>\n$maria-&gt;sex = \u2019female\u2019;<\/p>\n<p>echo $peter-&gt;sex; \/\/ This will output \u2018female\u2019<\/p>\n<p><strong>9. Interfaces:<\/strong><\/p>\n<p>PHP5 introduces \u2018interfaces\u2019 . An interface defines the methods a class must implement. All the methods defined in an interface must be public. An interface helps you design common APIs. It is not designed as a blueprint for classes, but just a way to standardize a common API. A big advantage of using interfaces is that a class can implement any number of interfaces. You can still only \u2018extend\u2019 on parent class, but you can \u2018implement\u2019 an unlimited number of interfaces.<\/p>\n<p><strong>10. New Functions:<\/strong><\/p>\n<p>PHP5 introduces new functions which are not found in PHP4. You can find the list of these new functions in the PHP manual.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This listing of the differences between PHP4 and PHP 5 is is probably something that is quite common, but it is always worth a review. PHP5 is a lot different than PHP4 with the main differences being in how it<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/\">Read more <span class=\"screen-reader-text\">Differences between PHP4 and PHP5<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-790","post","type-post","status-publish","format-standard","hentry","category-classes"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Differences between PHP4 and PHP5 - PHP Web Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Differences between PHP4 and PHP5 - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"This listing of the differences between PHP4 and PHP 5 is is probably something that is quite common, but it is always worth a review. PHP5 is a lot different than PHP4 with the main differences being in how it&hellip;Read more Differences between PHP4 and PHP5 &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-06-21T10:50:44+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Differences between PHP4 and PHP5\",\"datePublished\":\"2012-06-21T10:50:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/\"},\"wordCount\":830,\"commentCount\":0,\"articleSection\":[\"Classes\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/\",\"name\":\"Differences between PHP4 and PHP5 - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-06-21T10:50:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/differences-between-php4-and-php5\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Differences between PHP4 and PHP5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Differences between PHP4 and PHP5 - PHP Web Applications","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:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/","og_locale":"en_US","og_type":"article","og_title":"Differences between PHP4 and PHP5 - PHP Web Applications","og_description":"This listing of the differences between PHP4 and PHP 5 is is probably something that is quite common, but it is always worth a review. PHP5 is a lot different than PHP4 with the main differences being in how it&hellip;Read more Differences between PHP4 and PHP5 &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/","og_site_name":"PHP Web Applications","article_published_time":"2012-06-21T10:50:44+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Differences between PHP4 and PHP5","datePublished":"2012-06-21T10:50:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/"},"wordCount":830,"commentCount":0,"articleSection":["Classes"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/","url":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/","name":"Differences between PHP4 and PHP5 - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-06-21T10:50:44+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/differences-between-php4-and-php5\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Differences between PHP4 and PHP5"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=790"}],"version-history":[{"count":2,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/790\/revisions"}],"predecessor-version":[{"id":792,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/790\/revisions\/792"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}