Understand object oriented features in PHP

Object oriented features in PHP are listed and described below:

  • Visibility modifiers
  • Static Members
  • Abstract classes
  • Final classes and methods
  • Traits
  • Interfaces

If you want to learn about OOP concepts, please visit: OOP Concepts


  • Blueprint for the object. In other words, a class defines the object type.
  • class keyword is used to define a class.
  • use singular nouns when defining class names.
  • use one file per class definition and group all files in a relevant directory.
  • a class contains CONSTANTS, variables (aka properties), and functions (aka methods).


class Product {
    // class body

Class functions

  • get_declared_classes(): Array – get all declared classes in an array.
  • class_exists( $class_name ): bool – check if a class exists.


  • new keyword is used to create an object.
  • You should always define class first and include it in the script before instantiating an object.


// define class or include the class file before the following code.
$product1 = new Product;
$product2 = new Product;
// $product1 and $product2 are different. i.e., each object is unique.
if( $product1 === $product2 ){ 
    echo 'they are not unique';
} else {
    echo 'they are unique';

Object functions

  • get_class($object): bool – get the class name
  • is_a( $object_or_class, $class): bool – checks if the object is instance of class $class (or class is the child of $class).

Defining class properties and methods

  • Its always a good practice to use visibility specifiers for properties and methods. Default value is ‘public’.
  • public: can be accessed from outside of the class
  • protected: can not be accessed from outside the class. Can be inherited and overridden from inheriting class.
  • private: can only be accessed from inside the class and can not be overridden from inheriting class.


class Product {
    // visibility $variable_name = 'Default Value';
    public $product_name = 'Dummy Product';
    private $product_id = 1243;
    protected $product_sku = 'DUMMYSKU01';

    // visibility method_name( $args ){ /*body*/ }
    public function set_product( $name, $description, $sku ){
        $this->product_name = $name;
        $this->product_desc = $description;
        $this->product_sku = $sku;

Functions for properties and methods

  • get_class_vars( $string )
  • get_object_vars( $string )
  • property_exists( $mixed, $string )
  • get_class_methods( $mixed )
  • method_exists( $mixed, $string )

Understanding $this

  • $this always refers to current object.
  • Its used inside a class definition to access member functions and variables using arrow notation.
class Product {
    public $product_name = 'Dummy Product';
    public function get_product_name(){
        // notice the removal of '$' sign
        return $this->product_name;

    private function update_stock( $id, $quantity ){
        // method body
        // notice use of parenthesis
        $this->get_product( $id ); // member function call


Reference: PHP OOP

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.