SlideShare a Scribd company logo
OOP OBJECT ORIENTED PROGRAMMING
BASIC OOP CLASS Contains variable and functions working with these variable. A class has its own properties. An object is an instance of a class OBJECT 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. ABSTRACTION  ENCAPSULATION 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.  refers to a technique whereby you create program "objects" and then use these objects to build the functionality you need into your program. 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. POLYMORPHISM INHERITANCE 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.
CREATING A CLASS class class_name { var class_variable; function function_name()‏ } Class - is the keyword used to declare a class Class_name -specifies the class name Var - specifies that the variable is not just an ordinary variable but a property. Class_variable - specifies the variable name Function - is the keyword used to define a function Function _name()  - specifies the function name The syntax for declaring a class:-
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>
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.   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()‏ } new_class  – specifies the name of the derived class extends  – is the keyword used to derived a new class from the base class. class_name  - specifies the name of the class from which the new class is to be derived
EXTENDS 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.   <?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;;      }  }  ?>
CONSTRUCTOR A constructor is special function that has the same name as that of its class name. a constructor is called in the main program by using the  new  operator. When a constructor is declared, it provides a value to all the objects that are created in the class.
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>
PRIVATE <?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;;      }  }  ?>  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:
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.
KEYWORD -  new <?php  $daddy = new Bear;  ?>  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
KEYWORD - $this 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;. The $this prefix thus provides a convenient way to access variables and functions which are &quot;local&quot; to the class.  <?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;      }  }  ?>
KEYWORD - final <?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;  ?>  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).
Exception handling To handle OOP exceptions in php <?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;\n&quot;; } // Continue execution echo 'Hello World'; ?>

More Related Content

PPT
Oops in PHP
Mindfire Solutions
 
PPTX
Object oreinted php | OOPs
Ravi Bhadauria
 
PDF
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
PDF
OOP in PHP
Alena Holligan
 
PPT
Oops in PHP By Nyros Developer
Nyros Technologies
 
PPTX
Oop in-php
Rajesh S
 
PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Oops in PHP
Mindfire Solutions
 
Object oreinted php | OOPs
Ravi Bhadauria
 
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
OOP in PHP
Alena Holligan
 
Oops in PHP By Nyros Developer
Nyros Technologies
 
Oop in-php
Rajesh S
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 

What's hot (20)

PDF
Intermediate OOP in PHP
David Stockton
 
PPTX
Object oriented programming in php 5
Sayed Ahmed
 
PPTX
Introduction to PHP OOP
fakhrul hasan
 
PDF
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
ZIP
Object Oriented PHP5
Jason Austin
 
PDF
Object Oriented Programming in PHP
Lorna Mitchell
 
PPT
Class and Objects in PHP
Ramasubbu .P
 
PPT
PHP- Introduction to Object Oriented PHP
Vibrant Technologies & Computers
 
PPT
Introduction to OOP with PHP
Michael Peacock
 
PPTX
Oops in php
sanjay joshi
 
PPTX
Intro to OOP PHP and Github
Jo Erik San Jose
 
PPTX
Php oop presentation
Mutinda Boniface
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PDF
09 Object Oriented Programming in PHP #burningkeyboards
Denis Ristic
 
PPT
Intro to OOP and new features in PHP 5.3
Adam Culp
 
PDF
Cfphp Zce 01 Basics
Michael Girouard
 
PPTX
Only oop
anitarooge
 
PPT
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
Intermediate OOP in PHP
David Stockton
 
Object oriented programming in php 5
Sayed Ahmed
 
Introduction to PHP OOP
fakhrul hasan
 
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
Object Oriented PHP5
Jason Austin
 
Object Oriented Programming in PHP
Lorna Mitchell
 
Class and Objects in PHP
Ramasubbu .P
 
PHP- Introduction to Object Oriented PHP
Vibrant Technologies & Computers
 
Introduction to OOP with PHP
Michael Peacock
 
Oops in php
sanjay joshi
 
Intro to OOP PHP and Github
Jo Erik San Jose
 
Php oop presentation
Mutinda Boniface
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
09 Object Oriented Programming in PHP #burningkeyboards
Denis Ristic
 
Intro to OOP and new features in PHP 5.3
Adam Culp
 
Cfphp Zce 01 Basics
Michael Girouard
 
Only oop
anitarooge
 
Synapseindia object oriented programming in php
Synapseindiappsdevelopment
 
Demystifying Object-Oriented Programming - PHP[tek] 2017
Alena Holligan
 
Ad

Similar to Php Oop (20)

PPT
10 classes
Naomi Boyoro
 
PPT
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
PPTX
PHP OOP Lecture - 04.pptx
Atikur Rahman
 
PDF
Demystifying Object-Oriented Programming - ZendCon 2016
Alena Holligan
 
ODP
Object Oriented Programming With PHP 5 #2
Wildan Maulana
 
ODP
Perl Teach-In (part 2)
Dave Cross
 
PPTX
JavaScript Literacy
David Jacobs
 
PPTX
Object oriented programming in php
Aashiq Kuchey
 
PDF
Demystifying oop
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
PPT
PHP
webhostingguy
 
PPT
PHP Unit Testing
Tagged Social
 
PPTX
Ch8(oop)
Chhom Karath
 
PPT
Advanced php
hamfu
 
PPT
OOP
thinkphp
 
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
PDF
Demystifying Object-Oriented Programming - Midwest PHP
Alena Holligan
 
PPTX
OOP in PHP.pptx
switipatel4
 
10 classes
Naomi Boyoro
 
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
PHP OOP Lecture - 04.pptx
Atikur Rahman
 
Demystifying Object-Oriented Programming - ZendCon 2016
Alena Holligan
 
Object Oriented Programming With PHP 5 #2
Wildan Maulana
 
Perl Teach-In (part 2)
Dave Cross
 
JavaScript Literacy
David Jacobs
 
Object oriented programming in php
Aashiq Kuchey
 
Demystifying oop
Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
PHP Unit Testing
Tagged Social
 
Ch8(oop)
Chhom Karath
 
Advanced php
hamfu
 
OOP
thinkphp
 
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Demystifying Object-Oriented Programming - Midwest PHP
Alena Holligan
 
OOP in PHP.pptx
switipatel4
 
Ad

More from mussawir20 (20)

PPT
Php Operators N Controllers
mussawir20
 
PPT
Php Calling Operators
mussawir20
 
PPT
Database Design Process
mussawir20
 
PPT
Php Simple Xml
mussawir20
 
PPT
Php String And Regular Expressions
mussawir20
 
PPT
Php Sq Lite
mussawir20
 
PPT
Php Sessoins N Cookies
mussawir20
 
PPT
Php Rss
mussawir20
 
PPT
Php Reusing Code And Writing Functions
mussawir20
 
PPT
Php My Sql
mussawir20
 
PPT
Php File Operations
mussawir20
 
PPT
Php Error Handling
mussawir20
 
PPT
Php Crash Course
mussawir20
 
PPT
Php Basic Security
mussawir20
 
PPT
Php Using Arrays
mussawir20
 
PPT
Javascript Oop
mussawir20
 
PPT
Html
mussawir20
 
PPT
Javascript
mussawir20
 
PPT
Object Range
mussawir20
 
PPT
Prototype Utility Methods(1)
mussawir20
 
Php Operators N Controllers
mussawir20
 
Php Calling Operators
mussawir20
 
Database Design Process
mussawir20
 
Php Simple Xml
mussawir20
 
Php String And Regular Expressions
mussawir20
 
Php Sq Lite
mussawir20
 
Php Sessoins N Cookies
mussawir20
 
Php Rss
mussawir20
 
Php Reusing Code And Writing Functions
mussawir20
 
Php My Sql
mussawir20
 
Php File Operations
mussawir20
 
Php Error Handling
mussawir20
 
Php Crash Course
mussawir20
 
Php Basic Security
mussawir20
 
Php Using Arrays
mussawir20
 
Javascript Oop
mussawir20
 
Html
mussawir20
 
Javascript
mussawir20
 
Object Range
mussawir20
 
Prototype Utility Methods(1)
mussawir20
 

Recently uploaded (20)

PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
PDF
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
PDF
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
DevOps & Developer Experience Summer BBQ
AUGNYC
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂşnior
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
CIFDAQ'S Market Insight: BTC to ETH money in motion
CIFDAQ
 
Chapter 1 Introduction to CV and IP Lecture Note.pdf
Getnet Tigabie Askale -(GM)
 
Advances in Ultra High Voltage (UHV) Transmission and Distribution Systems.pdf
Nabajyoti Banik
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
DevOps & Developer Experience Summer BBQ
AUGNYC
 
This slide provides an overview Technology
mineshkharadi333
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
Comunidade Salesforce SĂŁo Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira JĂşnior
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Chapter 2 Digital Image Fundamentals.pdf
Getnet Tigabie Askale -(GM)
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
How-Cloud-Computing-Impacts-Businesses-in-2025-and-Beyond.pdf
Artjoker Software Development Company
 

Php Oop

  • 1. OOP OBJECT ORIENTED PROGRAMMING
  • 2. BASIC OOP CLASS Contains variable and functions working with these variable. A class has its own properties. An object is an instance of a class OBJECT 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. ABSTRACTION ENCAPSULATION 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. 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. 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. POLYMORPHISM INHERITANCE 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. CREATING A CLASS class class_name { var class_variable; function function_name()‏ } Class - is the keyword used to declare a class Class_name -specifies the class name Var - specifies that the variable is not just an ordinary variable but a property. Class_variable - specifies the variable name Function - is the keyword used to define a function Function _name() - specifies the function name The syntax for declaring a class:-
  • 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. 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. 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()‏ } new_class – specifies the name of the derived class extends – is the keyword used to derived a new class from the base class. class_name - specifies the name of the class from which the new class is to be derived
  • 6. EXTENDS 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. <?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. CONSTRUCTOR A constructor is special function that has the same name as that of its class name. a constructor is called in the main program by using the new operator. When a constructor is declared, it provides a value to all the objects that are created in the class.
  • 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. PRIVATE <?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;;     } } ?> 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. 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. KEYWORD - new <?php $daddy = new Bear; ?> 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. KEYWORD - $this 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;. The $this prefix thus provides a convenient way to access variables and functions which are &quot;local&quot; to the class. <?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. KEYWORD - final <?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; ?> 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).
  • 14. Exception handling To handle OOP exceptions in php <?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;\n&quot;; } // Continue execution echo 'Hello World'; ?>