Advertisement
sayful

PHP OOP 2

Sep 16th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.19 KB | None | 0 0
  1. /*-------------------------------------
  2. What's Object-Oriented Programming?
  3. _______________________________________*/
  4.  
  5. PHP is an object-oriented programming language, which means that you can create objects, which can contain variables and functions.
  6.  
  7. When talking about objects, you refer to variables belonging to these objects as properties (or attributes or fields), and functions are called methods.
  8.  
  9. These objects are essential when dealing with PHP, as almost everything is an object: for example, functions or arrays are objects, too!
  10. And this shows why we use objects: we can bundle our functions and data in one place, we can create objects easily using classes (object constructors), so we can create lots of instances (objects, which have been constructed via a class), which contain mostly the same data, except some little nuances.
  11.  
  12. On the right, there is a Person class and one instance stored in $me on line 32. Then the greet() method of the $me object is called and the result is echod on line 35.
  13.  
  14. <?php
  15.     // The code below creates the class
  16.     class Person {
  17.         // Creating some properties (variables tied to an object)
  18.         public $isAlive = true;
  19.         public $firstname;
  20.         public $lastname;
  21.         public $age;
  22.            
  23.         // Assigning the values
  24.         public function __construct($firstname, $lastname, $age) {
  25.             $this->firstname = $firstname;
  26.             $this->lastname = $lastname;
  27.             $this->age = $age;
  28.         }
  29.            
  30.         // Creating a method (function tied to an object)
  31.         public function greet() {
  32.             return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
  33.         }
  34.     }
  35.          
  36.     // Creating a new person called "boring 12345", who is 12345 years old ;-)
  37.     $me = new Person('boring', '12345', 12345);
  38.        
  39.     // Printing out, what the greet method returns
  40.     echo $me->greet();
  41. ?>
  42.  
  43.  
  44. /*-------------------------------------
  45. Objects in Real Life
  46. _______________________________________*/
  47.  
  48. How object-oriented programming is used in real life can be shown with a forum as an example:
  49.  
  50. Every forum user (object) has the same rights: he can log in and write (methods), can contain some settings (properties), but every user has a different name (another property).
  51.  
  52. Every user is created easily, as you create a new instance of a User class when you sign up. And as we've seen, there are some properties and methods that every instance has in common (such as logging in and writing), and there are some which are unique (such as each user's name).
  53.  
  54. And without object-oriented programming—OOP for short—this could not be done that easily. ;-)
  55.  
  56. Another example: on the right, there is a Person class, so every new Person has some properties, like $isAlive or $firstname, and a method greet().
  57.  
  58. Right now there is only one instance of the Person class: $me. But we'll reconstruct this class and you'll even create another instance of the class, so your name will be echod, too.
  59.  
  60. <?php
  61.     // The code below creates the class
  62.     class Person {
  63.         // Creating some properties (variables tied to an object)
  64.         public $isAlive = true;
  65.         public $firstname;
  66.         public $lastname;
  67.         public $age;
  68.            
  69.         // Assigning the values
  70.         public function __construct($firstname, $lastname, $age) {
  71.             $this->firstname = $firstname;
  72.             $this->lastname = $lastname;
  73.             $this->age = $age;
  74.         }
  75.            
  76.         // Creating a method (function tied to an object)
  77.         public function greet() {
  78.             return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
  79.         }
  80.     }
  81.          
  82.     // Creating a new person called "boring 12345", who is 12345 years old ;-)
  83.     $me = new Person('boring', '12345', 12345);
  84.        
  85.     // Printing out, what the greet method returns
  86.     echo $me->greet();
  87. ?>
  88.  
  89.  
  90.  
  91. /*-------------------------------------
  92. Building Your First Class
  93. _______________________________________*/
  94.  
  95. <?php
  96.     /*
  97.     * The basic class syntax looks like the following
  98.     * The class keyword means that you create a new class; the syntax is quite similar to the function syntax.
  99.     */
  100.     class Person{
  101.            
  102.     }
  103.     // Create new instances(objects) of this class using the following syntax
  104.     $teacher = new Person;
  105.     $student = new person;
  106.     /*
  107.     * The new keyword means that you create a new object and ensures that your arguments are added as properties,
  108.     * so it initializes the constructor (which we are going to deal with later)
  109.     * We don't need to pass in any arguments, as we haven't added any properties (which can store different values depending on the instance) quite yet.
  110.     */
  111. ?>
  112.  
  113. /*-------------------------------------
  114. Property Panic (1)
  115. _______________________________________*/
  116.  
  117. Properties are pieces of data bound to an object, and you can imagine an object as a bundle of information and actions.
  118.  
  119. <?php
  120.     //we first create a new class called 'Person'.
  121.     class Person{
  122.         //we add a property, $isAlive, and set its value to 'true'.
  123.         public $isAlive = true;
  124.         //we add three property, $firstname, $lastname & $age but don't store anything in it yet.
  125.         public $firstname;
  126.         public $lastname;
  127.         public $age;
  128.     }
  129.     //we create new instance of Person and store it in $teacher & $student.
  130.     $teacher = new Person();
  131.     $student = new person();
  132.     //Then we set the $firstname property of $student to the string "Sayful".
  133.     $teacher->firstname = "Sayful";
  134.     //Finally, we print out the two properties of $teacher.
  135.     echo $teacher->isAlive;
  136.     echo $teacher->firstname;
  137. ?>
  138.  
  139. /*-------------------------------------
  140. Property Panic (2)
  141. _______________________________________*/
  142.  
  143. Now we have some properties.
  144. But right now $teacher and $student are the same, which should be changed immediately, correct? :-)
  145.  
  146. The solution:
  147. we have to create a constructor to create different objects. This constructor is also a method, but you don't need to worry about this fact just yet.
  148.  
  149. The syntax:
  150. <?php
  151. public function __construct($prop1, $prop2) {
  152.   $this->prop1 = $prop1;
  153.   $this->prop2 = $prop2;
  154. }
  155. ?>
  156.  
  157. So you should remember the public keyword and the arrow notation.
  158.  
  159. Some new things:
  160.  
  161.     01. You're creating a function bound to a class (a method).
  162.  
  163.     02. The constructor method has to be called __construct().
  164.  
  165.     03. Finally, the weird way to assign the values: $this->prop1 = $prop1 means that the value you pass in the __construct() function via the new keyword is assigned to $this, which represents the object you are dealing with, and ->prop1 is the actual property of the object.
  166.  
  167.     04. By creating a new instance using the new keyword, you actually call this __construct() method, which constructs the object. And that's why we have to pass in some arguments when we create an instance of a class, since this is how the properties get set!
  168.  
  169. <?php
  170.     class Person{
  171.         public $isAlive = true;
  172.         public $firstname;
  173.         public $lastname;
  174.         public $age;
  175.            
  176.         // Use the three parameters to set the public properties $firstname, $lastname and $age
  177.         public function __construct($firstname,$lastname,$age){
  178.             $this->firstname = $firstname;
  179.             $this->lastname = $lastname;
  180.             $this->age = $age;
  181.         }
  182.     }
  183.     //Change your $teacher instantiation to store new Person("Sajeeb", "Hossain", 22)
  184.     $teacher = new Person('Sajeeb','Hossain',22);
  185.     $student = new person('Sayful','Islam',26);
  186.        
  187.     //echo the $firstname of $teacher
  188.     echo $teacher->firstname;
  189.     //echo the $age of $student
  190.     echo $student->age;
  191. ?>
  192.  
  193. /*-------------------------------------
  194. A Method to the Madness
  195. _______________________________________*/
  196.  
  197. Great work, now the hardest and longest part is behind us. :-)
  198.  
  199. As you've seen, methods—functions bundled into objects—have the following syntax:
  200.  
  201. <?php
  202. public function funcname($optionalParameter) {
  203.     // Do something
  204. }
  205. ?>
  206.  
  207. And now we know the __construct function is a special one, which is called when a new object is created using a new keyword.
  208. Furthermore, we've learnt we have to use the $this keyword, if we want to access some properties in a class.
  209.  
  210. So if we want a method to return a sentence containing the firstname, we would have to use $this->firstname. (As you see, there is no $ when you access a property in a class.)
  211.  
  212. Calling a method is similar to accessing a property, you just have to add the parentheses:
  213.  
  214. $obj1 -> meth1();
  215.  
  216. <?php
  217.     class Person{
  218.         public $isAlive = true;
  219.         public $firstname;
  220.         public $lastname;
  221.         public $age;
  222.            
  223.         public function __construct($firstname,$lastname,$age){
  224.             $this->firstname = $firstname;
  225.             $this->lastname = $lastname;
  226.             $this->age = $age;
  227.         }
  228.         public function greet(){
  229.             return "Hello, my name is ".$this->firstname." ".$this->lastname.". Nice to meet you!";  
  230.         }
  231.     }
  232.     $teacher = new Person('Sajeeb','Hossain',22);
  233.     $student = new person('Sayful','Islam',26);
  234.        
  235.     echo $teacher->greet().'<br>';
  236.     echo $student->greet();
  237. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement