{"id":1753,"date":"2021-05-21T06:39:07","date_gmt":"2021-05-21T06:39:07","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1753"},"modified":"2022-01-30T15:24:19","modified_gmt":"2022-01-30T15:24:19","slug":"php-oop","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-oop\/","title":{"rendered":"PHP OOP"},"content":{"rendered":"\n<p>This PHP OOP series helps you master Object-oriented Programming in PHP.<\/p>\n\n\n\n<p>PHP introduced object-oriented programming features since version 5.0. Object-Oriented programming is one of the most popular programming paradigms based on the concept of objects and classes.<\/p>\n\n\n\n<p>PHP OOP allows you to structure a complex application into a simpler and more maintainable structure.<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-1-objects-classes\" id='objects-classes'>Section 1. Objects &amp; Classes <a href=\"#objects-classes\" class=\"anchor\" id=\"objects-classes\" title=\"Anchor for Section 1. Objects &amp; Classes\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-objects\/\">Objects &amp; Classes <\/a>&#8211; learn the basic concepts of OOP including objects and classes.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-this\/\">The $this keyword<\/a> &#8211; help you understand PHP <code>$this<\/code> keyword and how to use it effectively.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-access-modifiers\/\">Access Modifiers: public vs. private<\/a> &#8211; explain to you the access modifiers in PHP and help you understand the differences between the <code>private<\/code> and <code>public<\/code> access modifiers.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-2-constructor-and-destructor\" id='constructor-and-destructor'>Section 2. Constructor and Destructor <a href=\"#constructor-and-destructor\" class=\"anchor\" id=\"constructor-and-destructor\" title=\"Anchor for Section 2. Constructor and Destructor\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-constructors\/\">Constructor<\/a> &#8211; explain to you the constructor concept and how to use it to initialize attributes.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-destructor\/\">Destructor<\/a> &#8211; learn how to use destructor to clean resources when the object is deleted.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-3-properties\" id='properties'>Section 3. Properties <a href=\"#properties\" class=\"anchor\" id=\"properties\" title=\"Anchor for Section 3. Properties\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-typed-properties\/\">Typed Properties<\/a> &#8211; show you how to add type hints to class properties.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-readonly-properties\/\">Readonly Properties<\/a> &#8211; use the readonly keyword to define readonly properties that can be initialized once within the class.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-4-inheritance\" id='inheritance'>Section 4. Inheritance <a href=\"#inheritance\" class=\"anchor\" id=\"inheritance\" title=\"Anchor for Section 4. Inheritance\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-inheritance\/\">Inheritance<\/a> &#8211; how to extend a class for code reuse.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-call-parent-constructor\/\">Call the parent constructor<\/a> &#8211; show you how to call the parent constructor from a child class&#8217;s constructor.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-override-method\/\">Overriding method<\/a> &#8211;  guide you on how to override a parent class&#8217;s method in the child class.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-protected\/\">Protected Access Modifier<\/a> &#8211; explain the protected access modifier and how to use protected properties and methods effectively.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-5-abstract-classes\" id='abstract-classes'>Section 5. Abstract classes <a href=\"#abstract-classes\" class=\"anchor\" id=\"abstract-classes\" title=\"Anchor for Section 5. Abstract classes\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-abstract-class\/\">Abstract Class<\/a> &#8211; guide you on abstract classes and how to use them effectively.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-6-interfaces\" id='interfaces'>Section 6. Interfaces <a href=\"#interfaces\" class=\"anchor\" id=\"interfaces\" title=\"Anchor for Section 6. Interfaces\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-interface\/\">Interface<\/a> &#8211; explain to you the interface concept and how to create interfaces.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-7-polymorphism\" id='polymorphism'>Section 7. Polymorphism <a href=\"#polymorphism\" class=\"anchor\" id=\"polymorphism\" title=\"Anchor for Section 7. Polymorphism\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-polymorphism\/\">Polymorphism<\/a> &#8211; explain the polymorphism concept and show you how to implement polymorphism in PHP using abstract classes or interfaces.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-8-traits\" id='traits'>Section 8. Traits <a href=\"#traits\" class=\"anchor\" id=\"traits\" title=\"Anchor for Section 8. Traits\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-traits\/\">Traits<\/a> &#8211; introduce you to traits.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-9-static-methods-properties\" id='static-methods-properties'>Section 9. Static methods &amp; properties <a href=\"#static-methods-properties\" class=\"anchor\" id=\"static-methods-properties\" title=\"Anchor for Section 9. Static methods &amp; properties\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-static-methods\/\">Static Methods and Properties<\/a> &#8211; show you how to use static methods and properties.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-class-constants\/\">Class constants<\/a> &#8211; learn how to define class constants using the const keyword.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-late-static-binding\/\">Late Static Binding<\/a> &#8211; introduce the late static binding concept and how to use it effectively.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-10-magic-methods\" id='magic-methods'>Section 10. Magic Methods <a href=\"#magic-methods\" class=\"anchor\" id=\"magic-methods\" title=\"Anchor for Section 10. Magic Methods\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-magic-methods\/\">Magic methods<\/a> &#8211; understand how the magic methods work in PHP.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__tostring\/\">__toString()<\/a> &#8211; return the string representation of an object.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__call\/\">__call()<\/a> &#8211; show you how to use the __call() magic method.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__callstatic\/\">__callStatic()<\/a> &#8211; show you how to use the __calStatic() magic method.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-__invoke\/\">__invoke()<\/a> &#8211; learn how to define a function object or function by implementing the __invoke() magic method.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-11-working-with-objects\" id='working-with-objects'>Section 11. Working with Objects <a href=\"#working-with-objects\" class=\"anchor\" id=\"working-with-objects\" title=\"Anchor for Section 11. Working with Objects\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-serialize\/\">Serialize Objects<\/a>&#8211; use the serialize() function to serialize an object into a binary string and how to use the __serialize() and __sleep() magic methods<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-unserialize\/\">Unserialize Objects<\/a> &#8211; guide you on how to use the unserialize() function to convert a serialized string into an object. Also, discuss the __wakeup() and __unserialize() magic methods.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-clone-object\/\">Cloning Objects<\/a> &#8211; show you how to copy an object.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-compare-objects\/\">Comparing Objects<\/a> &#8211; how to compare two objects.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-anonymous-class\/\">Anonymous class<\/a> &#8211; learn how to define a class without a declared name.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-12-namespaces\" id='namespaces'>Section 12. Namespaces <a href=\"#namespaces\" class=\"anchor\" id=\"namespaces\" title=\"Anchor for Section 12. Namespaces\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-namespace\/\">Namespace<\/a> &#8211; learn how to use namespaces to group the related classes.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-13-autoloading\" id='autoloading'>Section 13. Autoloading <a href=\"#autoloading\" class=\"anchor\" id=\"autoloading\" title=\"Anchor for Section 13. Autoloading\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-autoloading-class-files\/\">Autoloading Class files<\/a> &#8211; learn how to load classes automatically.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-composer-autoload\/\">Autoloading using Composer<\/a> &#8211; show you how to use Composer to autoload classes.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-14-exception-handling\" id='exception-handling'>Section 14. Exception Handling <a href=\"#exception-handling\" class=\"anchor\" id=\"exception-handling\" title=\"Anchor for Section 14. Exception Handling\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-try-catch\/\">try&#8230;catch<\/a> &#8211; show you how to use the try&#8230;catch statement to handle exceptions that may occur in your script.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-try-catch-finally\/\">try&#8230;catch&#8230;finally<\/a> &#8211; learn how to clean up the resources when an error occurs using the finally block.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-throw-exception\/\">Throw an exception<\/a>  &#8211; guide you on how to throw an exception using the throw statement.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-set-exception-handler\/\">Set an Exception Handler<\/a> &#8211; show you how to use the set_exception_handler function to set a global exception handler to catch the uncaught exceptions.<\/li><\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<h2 class=\"wp-block-heading\" id=\"section-15-class-object-functions\" id='class-object-functions'>Section 15. Class \/ Object Functions <a href=\"#class-object-functions\" class=\"anchor\" id=\"class-object-functions\" title=\"Anchor for Section 15. Class \/ Object Functions\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-class_exists\/\">class_exists<\/a> &#8211; return true if a class exists<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-method_exists\/\">method_exists<\/a> &#8211; return true if an object or a class has a specific method.<\/li><li><a href=\"https:\/\/phptutorial.net\/php-oop\/php-property_exists\/\">property_exists<\/a>  &#8211; return true if an object or a class has a specific property.<\/li><\/ul>\n<\/div><\/div>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1753\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/\"\n\t\t\t\tdata-post-title=\"PHP OOP\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1753\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-oop\/\"\n\t\t\t\tdata-post-title=\"PHP OOP\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This PHP OOP series helps you master PHP Object-oriented Programming and how to apply OOP in your applications.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1753","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1753"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753\/revisions"}],"predecessor-version":[{"id":2875,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1753\/revisions\/2875"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}