{"id":1218,"date":"2018-01-19T19:22:34","date_gmt":"2018-01-19T19:22:34","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/object-oriented-features-new-to-php5\/"},"modified":"2018-01-19T19:24:37","modified_gmt":"2018-01-19T19:24:37","slug":"object-oriented-features-new-to-php5","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/object-oriented-features-new-to-php5\/","title":{"rendered":"Object-Oriented Features New To PHP5"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By PHP Builder Staff<\/div>\n<div class=\"\">on February 1, 2008<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\"><a href=\"http:\/\/www.nostarch.com\/frameset.php?startat=oophp\" class=\"articleAuthor\">Peter Lavin<\/a><\/p>\n<div class=\"articlePara\"><font size=\"-2\"><i>This chapter is excerpted from Object-Oriented PHP: Concepts, Techniques, and Code from <a href=\"http:\/\/www.nostarch.com\/frameset.php?startat=oophp\">NoStarch Press<\/a><\/i>.<\/font><\/div>\n<div class=\"articlePara\">PHP 3 was released in mid-1998. Some basic object-oriented (OO) capabilities were included, more or less as an afterthought, to &#8220;provide new ways of accessing arrays.&#8221;<sup>1<\/sup> No significant changes were made to the object model when version 4 was released in mid-2000. The basics of object-oriented programming (OOP) were there&#8211;you could create a class and single inheritance was supported.<\/div>\n<div class=\"articlePara\">With the release of PHP 5 in 2004 there was plenty of room for improving PHP&#8217;s OO capabilities. At this point, Java, the most popular OO language<br \/>\nto date, had already been around for almost 10 years. Why did it take PHP so<br \/>\nlong to become a full-fledged OO language? The short answer is because<br \/>\nPHP is principally a web development language and the pressures of web<br \/>\ndevelopment have only recently pushed it in this direction.<\/p>\n<p>\n<font size=\"-2\"><sup>1<\/sup>  See Zeev Suraski, &#8220;Object-Oriented Evolution of PHP,&#8221; available at <a href=\"http:\/\/www.devx.com\/webdev\/\">www.devx.com\/webdev\/<\/a>.<\/font><\/p>\n<\/div>\n<div class=\"articlePara\">Support for objects has been grafted onto the language&#8211;you can choose to use objects or simply revert to procedural programming. That PHP is a<br \/>\nhybrid language should be viewed as something positive, not as a disadvantage.<br \/>\nThere are some situations where you will simply want to insert a snippet<br \/>\nof PHP and other situations where you will want to make use of its OO<br \/>\ncapabilities.<\/div>\n<div class=\"articlePara\">As I have already argued in Chapter 1, in some cases, an OO solution is the only solution. PHP 5 recognizes this fact and incorporates a full-blown<br \/>\nobject model, consolidating PHP&#8217;s position as the top server-side scripting<br \/>\nlanguage.<\/div>\n<div class=\"articlePara\">Like Chapter 2, this will be a chapter of broad strokes. I&#8217;ll give a general<br \/>\noverview of how the object model has been improved, and then I&#8217;ll get into<br \/>\nthe details using concrete examples in later chapters. I&#8217;ll also address the<br \/>\nissue of backward compatibility.<\/div>\n<h2>Access Modifiers<\/h2>\n<div class=\"articlePara\">Chapter 2 identified access modifiers as an essential element of an OO language. PHP 5 gives us everything we would expect in this area. In previous<br \/>\nversions of PHP there was no support for data protection, meaning that all<br \/>\nelements of a class were publicly accessible. This lack of access modifiers was probably the biggest disincentive to using objects in PHP 4.<\/div>\n<h2>NOTE<\/h2>\n<div class=\"articlePara\"><i>A notion closely related to data protection is <\/i>information hiding<i>. Access modifiers make information hiding possible by exposing an interface (as defined in Chapter 2). This is also referred to as <\/i>encapsulation<i> of an object.<\/i><\/div>\n<h2>Built-in Classes<\/h2>\n<div class=\"articlePara\">Every OOP language comes with some built-in classes, and PHP is no exception. PHP 5 introduces the Standard PHP Library (SPL), which provides a<br \/>\nnumber of ready-made classes and interfaces. As of version 5.1, depending<br \/>\nupon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces&#8211;a healthy increase from the number available in version 5.0.<\/div>\n<div class=\"articlePara\">Every OOP language comes with some built-in classes, and PHP is no exception. PHP 5 introduces the Standard PHP Library (SPL), which provides a<br \/>\nnumber of ready-made classes and interfaces. As of version 5.1, depending<br \/>\nupon how PHP is configured, all in all, there are well over 100 built-in classes and interfaces&#8211;a healthy increase from the number available in version 5.0.<\/div>\n<div class=\"articlePara\">Having ready-made objects speeds up development, and native classes<br \/>\nwritten in C offer significant performance advantages. Even if these built-in<br \/>\nclasses don&#8217;t do exactly what you want, they can easily be extended to suit<br \/>\nyour needs.<\/div>\n<h2>NOTE<\/h2>\n<div class=\"articlePara\"><i>There are far too many classes for us to deal with all of them in this book, and some are still not very well documented. We&#8217;ll focus on the classes that are especially noteworthy.<\/i><\/div>\n<h2>Exceptions<\/h2>\n<div class=\"articlePara\">All OOP languages support <i>exceptions<\/i>, which are the OO way of handling errors. In order to use exceptions, we need the keywords try, catch, and throw. A <i>try block<\/i> encloses code that may cause an error. If an error occurs, it is <b>12<\/b> thrown and caught by a <i>catch block<\/i>. The advantage of exceptions over errors is that exceptions can be handled centrally, making for much cleaner code. <\/div>\n<div class=\"articlePara\">Exceptions also significantly reduce the amount of error-trapping code you need to write, which offers welcome relief from an uninspiring task. Also, hav-<br \/>\ning a built-in exception class makes it very easy to create your own customized exceptions through inheritance.<\/div>\n<h2>Database Classes<\/h2>\n<div class=\"articlePara\">Because PHP is all about building dynamic web pages, database support is all-important. PHP 5 introduces the mysqli (MySQL Improved) extension with<br \/>\nsupport for the features of MySQL databases versions 4.1 and higher. You<br \/>\ncan now use features such as prepared statements with MySQL, and you can<br \/>\ndo so using the built-in OO interface. In fact, anything you can do procedur-<br \/>\nally can also be done with this interface. <\/div>\n<div class=\"articlePara\">SQLite is a database engine that is incorporated directly into PHP. It is not a general-purpose database like MySQL, but it is an ideal solution in<br \/>\nsome situations, in many cases producing faster, leaner, and more versatile<br \/>\napplications. Again an entirely OO interface is provided.<\/div>\n<div class=\"articlePara\">PHP versions 5.1 and higher also bundle PHP Data Objects (PDO) with<br \/>\nthe main PHP distribution. If you need to communicate with several differ-<br \/>\nent database back ends, then this package is the ideal solution. PDO&#8217;s<br \/>\ncommon interface for different database systems is only made possible by<br \/>\nthe new object model.<\/div>\n<div class=\"articlePara\">Given the importance of databases, we&#8217;ll deal with them extensively in this book. We&#8217;ll develop a MySQL database class starting with Chapter 9.<br \/>\nIn Chapter 15 we&#8217;ll look at SQLite, and in Chapter 16 we&#8217;ll discuss PDO.<\/div>\n<h2>Web Services<\/h2>\n<div class=\"articlePara\">In PHP 5 all Extensible Markup Language (XML) support is provided by<br \/>\nthe libxml2 XML toolkit (www.xmlsoft.org). The underlying code for the<br \/>\nSimple API for XML (SAX) and for the Document Object Model (DOM)<br \/>\nhas been rewritten, and DOM support has been brought in line with the<br \/>\nstandard defined by the World Wide Web Consortium.<\/div>\n<div class=\"articlePara\">Unified treatment of XML under libxml2 makes for a more efficient and<br \/>\neasily maintained implementation. This is particularly important because sup-<br \/>\nport for XML under PHP 4 is weak, and web services present many problems<br \/>\nthat require an OO approach.<\/div>\n<div class=\"articlePara\">Under PHP 4, creating a SOAP client and reading an RSS feed are<br \/>\nchallenging programming tasks that require creating your own classes or<br \/>\nmaking use of external classes such as NuSOAP (http:\/\/sourceforge.net\/<br \/>\nprojects\/nusoap). There&#8217;s no such need in PHP 5. In Chapter 12, you&#8217;ll<br \/>\nsee just how easy these tasks are using the built-in SOAPClient class and<br \/>\nSimpleXMLElement. Again it&#8217;s the improved object model that makes this<br \/>\npossible.<\/div>\n<h2>Reflection Classes<\/h2>\n<div class=\"articlePara\">The reflection classes included in PHP 5 provide ways to introspect objects<br \/>\nand reverse engineer code. The average web developer might be tempted<br \/>\nto ignore these classes, but Chapter 14 shows how useful they are for auto-<br \/>\nmating a task that most developers approach with little enthusiasm: the<br \/>\ncreation of documentation.<\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>PHP3 was released in mid-1998 with only basic object-oriented (OO) capabilities. With the release of PHP5 in 2004 there was plenty of room for<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1218","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1218"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1218\/revisions"}],"predecessor-version":[{"id":3121,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1218\/revisions\/3121"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}