Advertisement
sayful

PHP OOP 1

Jul 12th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.90 KB | None | 0 0
  1. <?php
  2. /************************************************
  3. * What is object-oriented programming?
  4. => Object-oriented programming, or OOP, takes things further by storing both the data structures, and the functions that work on the data, in a single entity known as an object. Rather than passing data around in function calls, you instead load the data you want to work with into an object, and then call the object’s functions to manipulate the object’s data and produce the result you want.
  5. ************************************************/
  6.  
  7.  
  8. /************************************************
  9.  => Four fundamental OOP concepts:
  10.     01. classes
  11.     02. objects
  12.     03. properties
  13.     04. methods
  14. ************************************************/
  15.  
  16.  
  17. /************************************************
  18. 01. CLASSES
  19.  
  20. => A class is a blueprint for objects. It’s a chunk of code that defines:
  21.  
  22.         A. The types of data that objects created from the class will store, and
  23.         B. The functions that those objects will contain.
  24.  
  25.     When you build your object-oriented application, you typically create one or more classes representing various types of entities in your app.
  26.  
  27.     For example, if you’re writing a forum application, you might create classes called Forum, Topic, Post, and Member.
  28. ************************************************/
  29. class ClassName
  30. {
  31. // (class definition goes here)
  32. }
  33.  
  34. /************************************************
  35. 02. OBJECTS
  36.  
  37. => An object is a special type of variable that is created from a class. It contains actual data, and you can call the object’s functions to do stuff to that data. You can create as many objects as you like from a single class. Each object functions independently of the others, even if they all come from the same class.
  38.  
  39. To use a real-world analogy:
  40.  
  41.     => A class is like a blueprint for a car. It defines how cars created from the blueprint will look and behave, but it’s still an abstract concept.
  42.  
  43.     => An object is like a real car created from the blueprint. It has real properties (such as how fast it’s going), and real behaviors (like “accelerate” and “brake”).
  44.  
  45. ************************************************/
  46. $objects = new ClassName();
  47.  
  48.  
  49. /************************************************
  50. 03. PROPERTIES
  51.  
  52. => The data values that an object holds are stored in special variables known as properties.
  53. => An object’s properties are closely tied to the object. Although all objects created from a given class have the same properties, one object’s properties can have different values to another object’s properties.
  54.  
  55. ************************************************/
  56. class ClassName                 //CLASSES
  57. {
  58.     public $porperties = "";    //PROPERTIES
  59. }
  60.  
  61. $objects = new ClassName();     //OBJECTS
  62. $objects->porperties = "Fred";  //PROPERTIES
  63. echo $objects->porperties;      // Displays "Fred"
  64.  
  65.  
  66.  
  67. /************************************************
  68. 04. METHODS
  69.  
  70. => The functions that are defined within a class — and used in an object — are known as methods.
  71. => In many ways, they’re just like regular functions — you can pass values to them, they can contain local variables, and they can return values. However, an object’s methods typically work mostly with the object’s properties. For example, a login() method to log a member into your forums might set the member’s loggedIn property to true.
  72.  
  73. ************************************************/
  74. class ClassName
  75. {
  76.     public function methodName() {
  77.         // (Method's code here)
  78.     }
  79.     private function methodName() {
  80.         // (Method's code here)
  81.     }
  82.     protected function methodName() {
  83.         // (Method's code here)
  84.     }
  85. }
  86.  
  87. /************************************************
  88. Visibility (public, private, protected) of PROPERTIES & METHODS
  89.  
  90. => The visibility of class members, (properties, methods), relates to how that member may be manipulated within, or from outside the class. Three levels of visibilty exist for class members.
  91.  
  92.     public      ==>>    By default, all class members are public. This means if you do not declare a property or method within the class, it will be public. It is good practice to declare the visibility of class members for the sake of readability for yourself and others. It is much easier for another programmer to see your intentions. This will also future proof your scripts should access modifiers be deprecated.
  93.  
  94.     private     ==>>    If you make a property or method private. This ensures that the property or method is only available within the class itself. It is private to the calling class.
  95.  
  96.     protected   ==>>    To access a parent method or property from a child class you need to use the protected keyword. Like the private keyword, protetected methods and properties are available only to the class that created them. But unlike private, protected methods and properties are visible from a parent class.
  97.  
  98.  
  99. ************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement