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
PPTX
Object oreinted php | OOPs
PDF
A Gentle Introduction To Object Oriented Php
PDF
OOP in PHP
PPT
Oops in PHP By Nyros Developer
PPTX
Oop in-php
PPT
Class 7 - PHP Object Oriented Programming
Oops in PHP
Object oreinted php | OOPs
A Gentle Introduction To Object Oriented Php
OOP in PHP
Oops in PHP By Nyros Developer
Oop in-php
Class 7 - PHP Object Oriented Programming

What's hot (20)

PDF
Intermediate OOP in PHP
PPTX
Object oriented programming in php 5
PPTX
Introduction to PHP OOP
PDF
Object Oriented Programming with PHP 5 - More OOP
ZIP
Object Oriented PHP5
PDF
Object Oriented Programming in PHP
PPT
Class and Objects in PHP
PPT
PHP- Introduction to Object Oriented PHP
PPT
Introduction to OOP with PHP
PPTX
Oops in php
PPTX
Intro to OOP PHP and Github
PPTX
Php oop presentation
PPT
PHP - Introduction to Object Oriented Programming with PHP
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PPT
Intro to OOP and new features in PHP 5.3
PDF
Cfphp Zce 01 Basics
PPTX
Only oop
PPT
Synapseindia object oriented programming in php
PDF
Demystifying Object-Oriented Programming - PHP[tek] 2017
Intermediate OOP in PHP
Object oriented programming in php 5
Introduction to PHP OOP
Object Oriented Programming with PHP 5 - More OOP
Object Oriented PHP5
Object Oriented Programming in PHP
Class and Objects in PHP
PHP- Introduction to Object Oriented PHP
Introduction to OOP with PHP
Oops in php
Intro to OOP PHP and Github
Php oop presentation
PHP - Introduction to Object Oriented Programming with PHP
09 Object Oriented Programming in PHP #burningkeyboards
Intro to OOP and new features in PHP 5.3
Cfphp Zce 01 Basics
Only oop
Synapseindia object oriented programming in php
Demystifying Object-Oriented Programming - PHP[tek] 2017
Ad

Similar to Php Oop (20)

PPT
10 classes
PPTX
OOP Day 1
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PPTX
Ch8(oop)
ODP
Object Oriented Programming With PHP 5 #2
PPTX
Object oriented programming in php
PPT
DOCX
Oops concept in php
PPTX
Lecture-10_PHP-OOP.pptx
PDF
Oop in php tutorial
PDF
oop_in_php_tutorial_for_killerphp.com
PDF
oop_in_php_tutorial_for_killerphp.com
PDF
Oop in php_tutorial_for_killerphp.com
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPT
PHP 5 Boot Camp
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
PPTX
PDF
OOP in PHP
10 classes
OOP Day 1
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Ch8(oop)
Object Oriented Programming With PHP 5 #2
Object oriented programming in php
Oops concept in php
Lecture-10_PHP-OOP.pptx
Oop in php tutorial
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PHP 5 Boot Camp
Lecture 17 - PHP-Object-Orientation.pptx
OOP in PHP
Ad

More from mussawir20 (20)

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

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Empathic Computing: Creating Shared Understanding
PDF
Modernizing your data center with Dell and AMD
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
A Presentation on Artificial Intelligence
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation theory and applications.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
Empathic Computing: Creating Shared Understanding
Modernizing your data center with Dell and AMD
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Weekly Chronicles - August'25 Week I
A Presentation on Artificial Intelligence
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation theory and applications.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf

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'; ?>