Php Oop

4,882 views

Published on

Published in: Technology
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
4,882
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
278
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Php Oop

  1. 1. OOP OBJECT ORIENTED PROGRAMMING
  2. 2. BASIC OOP <ul><li>CLASS </li></ul>Contains variable and functions working with these variable. A class has its own properties. An object is an instance of a class <ul><li>OBJECT </li></ul>Consist of data structures and functions for manipulating the data. Data structure refers to the type of data while function refers to the operation applied to the data structure. An object is a self contained run time entity. <ul><li>ABSTRACTION </li></ul><ul><li>ENCAPSULATION </li></ul>Process of selecting the commons features from different function and objects. The functions those perform same actions can be joined into a single function using abstraction. Process of joining data and objects into another object. It hides the details of data. <ul><li>refers to a technique whereby you create program &quot;objects&quot; and then use these objects to build the functionality you need into your program. </li></ul>Process of using a single function or an operator in different ways. The behavior of that function will depend on the type of the data used in it. <ul><li>POLYMORPHISM </li></ul><ul><li>INHERITANCE </li></ul>Process of creating a new class from the existing class. The new class is called derived class while the existing class from which the new class is derived is called the base class.
  3. 3. CREATING A CLASS class class_name { var class_variable; function function_name()‏ } <ul><li>Class - is the keyword used to declare a class </li></ul><ul><li>Class_name -specifies the class name </li></ul><ul><li>Var - specifies that the variable is not just an ordinary variable but a property. </li></ul><ul><li>Class_variable - specifies the variable name </li></ul><ul><li>Function - is the keyword used to define a function </li></ul><ul><li>Function _name() - specifies the function name </li></ul>The syntax for declaring a class:-
  4. 4. EXAMPLE <?php // PHP 5 // class definition class Bear { // define properties public $name; public $weight; public $age; public $sex; public $colour; // define methods public function eat() { echo $this->name.&quot; is eating... &quot;; } public function run() { echo $this->name.&quot; is running... &quot;; } public function kill() { echo $this->name.&quot; is killing prey... &quot;; } public function sleep() { echo $this->name.&quot; is sleeping... &quot;; } } ?> </body> </html>
  5. 5. <ul><li>A new class can be inherited from an existing class. The new class uses the properties of the parent class along with its own properties. </li></ul>CREATING A CLASS - INHERITED CLASS The syntax for inheriting a new class is: class new_class extends class_name { var class_variable; function function_name()‏ } <ul><li>new_class – specifies the name of the derived class </li></ul><ul><li>extends – is the keyword used to derived a new class from the base class. </li></ul><ul><li>class_name - specifies the name of the class from which the new class is to be derived </li></ul>
  6. 6. EXTENDS <ul><li>The extends keyword is used to extend a parent class to a child class. All the functions and variables of the parent class immediately become available to the child class. </li></ul><?php // PHP 5 // class definition class Bear { // define properties public $name; public $weight; public $age; public $sex; public $colour; // define methods public function eat() { echo $this->name.&quot; is eating... &quot;; } public function run() { echo $this->name.&quot; is running... &quot;; } public function kill() { echo $this->name.&quot; is killing prey... &quot;; } public function sleep() { echo $this->name.&quot; is sleeping... &quot;; } } ?> </body> </html> // extended class definition class PolarBear extends Bear {     // constructor     public function __construct() {         parent::__construct();         $this->colour = &quot;white&quot;;         $this->weight = 600;     }     // define methods     public function swim() {         echo $this->name.&quot; is swimming... &quot;;     } } ?>
  7. 7. CONSTRUCTOR <ul><li>A constructor is special function that has the same name as that of its class name. </li></ul><ul><li>a constructor is called in the main program by using the new operator. </li></ul><ul><li>When a constructor is declared, it provides a value to all the objects that are created in the class. </li></ul>
  8. 8. CONSTRUCTOR OUTPUT:- Baby Bear is brown and weighs 100 units at birth <?php // PHP 5 // class definition class Bear { // define properties public $name; public $weight; public $age; public $colour; // constructor public function __construct() { $this->age = 0; $this->weight = 100; $this->colour = &quot;brown&quot;; } // define methods } ?> </body> </html> <?php // create instance $baby = new Bear; $baby->name = &quot;Baby Bear&quot;; echo $baby->name.&quot; is &quot;.$baby->colour.&quot; and weighs &quot;.$baby->weight.&quot; units at birth&quot;; ?> </body> </html>
  9. 9. PRIVATE <ul><li><?php // PHP 5 // class definition class Bear {     // define properties     public $name;     public $age;     public $weight;       private $_lastUnitsConsumed;     // constructor     public function __construct() {         $this->age = 0;         $this->weight = 100;         $this->_lastUnitsConsumed = 0;     }          // define methods     public function eat($units) {         echo $this->name.&quot; is eating &quot;.$units.&quot; units of food... &quot;;         $this->weight += $units;         $this->_lastUnitsConsumed = $units;     }     public function getLastMeal() {         echo &quot;Units consumed in last meal were &quot;.$this->_lastUnitsConsumed.&quot; &quot;;     } } ?> </li></ul>makes it possible to mark class properties and methods as private, which means that they cannot be manipulated or viewed outside the class definition. This is useful to protect the inner workings of your class from manipulation by object instances. Consider the following example, which illustrates this by adding a new private variable, $_lastUnitsConsumed, to the Bear() class:
  10. 10. PUBLIC,PROTECTED <?php // PHP 5 // class definition class Bear {     // define public properties     public $name;     public $age;     // more properties     // define public methods     public function eat() {         echo $this->name.&quot; is eating... &quot;;         // more code     }     // more methods } ?> By default, class methods and properties are public; this allows the calling script to reach inside your object instances and manipulate them directly.
  11. 11. KEYWORD - new <ul><li><?php $daddy = new Bear; ?> </li></ul>mean &quot;create a new object of class Bear() and assign it to the variable $daddy &quot;. <?php $daddy->name = &quot;Daddy Bear&quot;; ?> mean &quot;assign the value Daddy Bear to the variable $name of this specific instance of the class Bear()&quot;, <?php $daddy->sleep(); ?> would mean &quot;execute the function sleep() for this specific instance of the class Bear()&quot;. Note the -> symbol used to connect objects to their properties or methods, and the fact that the $ symbol is omitted when accessing properties of a class instance
  12. 12. KEYWORD - $this <ul><li>the $this prefix indicates that the variable to be modified exists within the class - or, in English, &quot;add the argument provided to eat() to the variable $weight within this object&quot;. </li></ul><ul><li>The $this prefix thus provides a convenient way to access variables and functions which are &quot;local&quot; to the class. </li></ul><?php // PHP 5 // class definition class Bear {     // define properties     public $name;     public $weight;     // define methods     public function eat($units) {         echo $this- >name.&quot; is eating &quot;.$units.&quot; units of food... &quot;;         $this->weight += $units;     } } ?>
  13. 13. KEYWORD - final <ul><li><?php // class definition fina l class Bear {     // define properties     // define methods } // extended class definition // this will fail because Bear() cannot be extended class PolarBear extends Bear {         // define methods } // create instance of PolarBear() // this will fail because Bear() could not be extended $bob = new PolarBear; $bob->name = &quot;Bobby Bear&quot;; echo $bob->weight; ?> </li></ul><ul><li>To prevent a class or its methods from being inherited, use the final keyword before the class or method name (this is new in PHP 5 and will not work in older versions of PHP). </li></ul>
  14. 14. Exception handling <ul><li>To handle OOP exceptions in php </li></ul><?php try { $error = 'Always throw this error'; throw new Exception($error); // Code following an exception is not executed. echo 'Never executed'; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), &quot; &quot;; } // Continue execution echo 'Hello World'; ?>

×