basic oops concept of php

Download Basic Oops concept of PHP

Post on 04-Jul-2015




1 download

Embed Size (px)


This slide contains the information regarding the basic oops concepts of PHP


  • 1. PHP Object Oriented Concepts

2. PHP 4, PHP 5 & PHP 6 There are substantial differences between PHP 4 and PHP 5. Most of the hype was around the new object model, which was completely rewritten in PHP5. The PHP 5 version is much more complete, and performs much better as well. In PHP 4, objects were really just primitive data types, and were referenced by value. In an attempt to retain as much backward compatibility as possible in PHP 5 allows compatibility with the version 4 methods. With the release of PHP 5 in 2004, PHP programmers finally had the power to code like the Java and C#, PHP finally had a complete OOP infrastructure. PHP 6 has more features of object Oriented Concepts. 3. Step by Step Process The difference between building a PHP application the old fashioned (procedural) way versus the OOP way. What the basic OOP principles are, and how to use them in PHP? When to use OOP in your PHP scripts? 4. Object Oriented PHP How to develop a OO PHP ?? to get into this we are going to divide the process into 22 steps by which we can get a basic idea to develop an application in OOP Concepts. STEP 1 First lets create 2 PHP files index.php class_lib.php OOP is all about creating modular code, so our object oriented PHP code will be contained in dedicated files that we will then insert into our normal PHP page using PHP 'includes'. In this case, all our OO PHP code will be in the PHP file: class_lib.php In OOP codes revolves around a 'class', Classes are the templates that are used to define objects. STEP 2 Create a simple PHP class (in class_lib.php) Instead of having a bunch of functions, variables and code floating around, to design our PHP scripts in the OOP way, we need to create our own classes. keyword 'class' 5. STEP 2 ( conti ...) STEP 3 (add data to your class) Classes are the blueprints for php objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: 'object'. When you create a variable inside a class, it is called a 'property'. 6. STEP 4 (add functions/methods to your class) Functions also referred by different name when created inside a class - they are called 'methods'. A class's methods are used to manipulate its own data / properties. 7. STEP 5 (getter and setter functions) We've created two interesting functions/methods: get_name() and set_name(). These methods follow a common OOP convention that you see in many languages (including Java and Ruby) - where you create methods to 'set' and 'get' properties in a class. NOTE : Another convention (a naming convention,) is that getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'. 8. STEP 6 (The '$this' variable) $this->name = $new_name; $this is a built-in variable which points to the current object. Or in other words, $this is a special self-referencing variable. We use $this to access properties and to call other methods of the current class. STEP 7 (Use our class in our main PHP page : index.php ) We should not create the PHP classes in our main page, else it will break the main purpose of building applications in OOP. So in index.php include the file ( class_lib.php ) 9. STEP 8 ( Instantiate/create your object ) Classes are the blueprints/templates of php objects. Classes don't actually become objects until you do something called: instantiation. When you instantiate a class, you create an instance of it ... thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory. What memory? The server's memory of course! Note: The variable $obj_mfsemp becomes a handle/reference to our newly created mfs_employee class. It is a 'handle', because we will use $obj_mfsemp to control and use the mfs_employee class. 10. STEP 9 ( new keyword ) To create an object out of a class, you need to use the 'new' keyword. When creating/instantiating a class, we can optionally add brackets to the class name, as below example. To be clear, we can see in the code below how we create multiple objects from the same class. From the PHP's engine point of view, each object is its own entity. 11. STEP 10 ( Set an objects properties ) Now that we've created/instantiated our two separate 'mfs_employee' objects, we can set their properties using the methods (the setters) we created. Please keep in mind that though both our mfs_employee objects ($obj_mfsemp1 and $obj_mfsemp2) are based on the same 'mfs_employee' class, as far as php is concerned, they are totally different objects. 12. STEP 11 ( Accessing an object's data ) Now we use the getter methods to access the data held in our objects this is the same data we inserted into our objects using the setter methods. When accessing methods and properties of a class, we use the arrow (->) operator. 13. In this short period of time, we have covered Designed a PHP class. Generate/created a couple of objects based on your class. Inserted data into your objects. Retrieved data from your objects. Lets now focus on PHP OBJECT. 14. STEP 12 ( Directly accessing properties - don't do it! ) We don't have to use methods to access objects properties; you can directly get to them using the arrow operator (->) and the name of the variable. For example: with the property $name (in object $obj_mfsemp1,) we can get its' value like : NOTE : Though doable, it is considered bad practice to do it because it can lead to trouble down the road. We should use getter methods instead. 15. STEP 13 ( Constructor ) All objects can have a special built-in method called a 'constructor'. Constructors allow you to initialize your object's properties (give values to properties) when we instantiate (create) an object. Note: If you create a __construct() function PHP will automatically call the __construct() method/function when you create an object from your class. The 'construct' method starts with two underscores (__) and the word 'construct'. 16. STEP 14 ( Create an object with a constructor ) Now that we've created a constructor method, we can provide a value for the $name property when we create our objects for the class mfs_employee. We 'feed' the constructor method by providing a list of arguments (like we do with a function) after the class name at the time of object declaration. Not a constructor When have constructor 17. STEP 15 ( access modifiers ) One of the fundamental principles in OOP is 'encapsulation'. The idea is that we create cleaner better code, if you restrict access to the data structures (properties) in our objects. Encapsulation : Storing data/properties and functions/methods in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it. We restrict access to class properties using something called 'access modifiers'. There are 3 access modifiers: 1. public 2. private 3. protected 'Public' is the default modifier. 18. STEP 15 ( access modifiers ) conti... 19. STEP 16 ( Restricting access to properties ) Properties declared as 'public' have no access restrictions, meaning anyone can access them. When you declare a property as 'private', only the same class can access the property. When a property is declared 'protected', only the same class and classes derived from that class can access the property - this has to do with inheritance 20. STEP 17 ( Restricting access to methods ) Like properties, you can control access to methods using one of the three access modifiers: 1. public 2. protected 3. private Since the method getpin() is 'private', the only place you can use this method is in the same class - typically in another method in class. If we wanted to call/use this method directly in our PHP pages, we need to declare it as 'public'. 21. STEP 18 ( Inheritance - reusing code the OOP way ) Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class or many other classes. Why do it? Doing this allows help to efficiently reuse the code found in our base class. Say, you wanted to create a new 'sales_people' class since we can say that 'mfs_employee' is a type/kind of 'peoples', they will share common properties and methods. In this type of situation, inheritance can make our code lighter because we are reusing the same code in two different classes. 1. You only have to type the code out once. 2. The actual code being reused, can be reused in many classes but it is only typed out in one place conceptually, this is sort-of like PHP includes(). 22. STEP 18 ( Inheritance - reusing code the OOP way ) conti.. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } 23. STEP 19 ( Inheritance - reusing code the OOP way how to access ) Because the class 'sales_people' is based on the class ' mfs_employee ', 'sales_people' automatically has all the public and protected, properties and methods of 'mfs_employee' class. Notice how we are able to use set_name() in 'sales_people', even though we did not declare that method in the 'sales_people' class. That's because we already created set_name() in the class 'mfs_employee'. Note: the 'sales_people' class is called children the 'base' class or the 'mfs_employee' class because it's the class that the 'sales_people' is based on. This class hierarchy can become important down the road when our projects become more complex. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } }