PHP: Object Oriented Programing Tutorial

By Xah Lee. Date:

This page is a basic tutorial on Object in PHP.

Class Definition: Keyword “class”

A “class” is basically a boxed set of functions and data. The boxed {function, data} are called the class's “members”.

The syntax of “class” is like definition of a function. After you defined a classe, you create “objects” to use it.

Use the class keyword to define a class.

<?php

// define a class
class AA
{
  // data
  public $ss = 3;

  // method
  public function ff() {
    echo 4;
  }
}

Creating a Object, Calling a Method

$object_name = new class_name(); → creates a object.

$object_name->method_name(…) → calls method method_name.

<?php

class AA
{
  public function ff() { return 4; }
}

// create a object
$xx = new AA();

// call a method
echo $xx->ff(); // prints 4

Calling a Method without Instantiation

A method can be called without creating a object, by this syntax: class_name::method_name.

<?php
class AA {
  public function ff() { return 4; }
}

echo AA::ff(); // prints 4

Use {public, protected, private} for Class Members

In PHP, variables inside a class are called “properties”.

They must be defined by using one of the keywords {public, protected, private}. (using var to declare is deprecated. If used, it's equivalent to public.)

<?php
class Bee
{
    public $pub = 'pub';
    protected $pro = 'pro';
    private $pri = 'privy';

}

$ye = new Bee();
echo $ye->pub; // prints “pub”

echo $ye->pro; // Fatal error
echo $ye->pri; // Fatal error

Printing Objects: var_dump()

var_dump can be used to print objects.

<?php
class AA
{
  public $ss = 3;
  public function ff() { return 4; }
}

$xx = new AA();
var_dump($xx);

/* 
prints:

object(AA)#1 (1) {
  ["ss"]=>
  int(3)
}
*/

Object Self-Reference: $this

When defining a class, you can use a special variable $this to stand for the class as a initialized object. It's called a “pseudo-variable”.

<?php
class Aa
{
  public $ss = 3;
  public function ff() { return $this->ss; }
}

$xx = new Aa();
echo $xx->ff(); // prints 3

Note the syntax: there is no dollar sign in front of the variable.

Following is example, showing that $this stands for a object of the class, not the class itself.

<?php
class Aa
{
  function ff()
  {
    if (isset($this)) {
      echo "yes \n";
    } else {
      echo "no \n";
    }
  }
}

Aa::ff();                        // prints: no

$a = new Aa();
$a->ff();                       // prints: yes

Note: for properties declared with static or const (constant), you have to use the form self::$property_name.

Constructor

A constructor is a method that is automatically called when a object is created. It usually is used to initialize variables. Sometimes it's called initializer.

You can create a constructor by defining a function with the keyword __construct, like this: function __construct(…).

<?php
class A1
{
  // create a constructor
  function __construct() { echo 'meee'; }
}

$xx = new A1(); // prints “meee”

Old style constructor are methods having same name as class. PHP will consider it constructor only if the class doesn't have __construct.

<?php
class A1
{
  function A1() { echo 'meee'; } // old style constructor are methods having same name as class
}
$xx = new A1(); // prints “meee”

Extending a Class (Subclass, Inheritance)

You can create a new class that has the same data and methods of another class, without copy/paste code. This is called “inheritance”, or “subclass”, or “extending a class”.

To extend a class, use keyword extends.

<?php
class A1
{
  public $ss = 3;
  public function ff() { return 4; }
}

class B1 extends A1
{
}

$xx = new B1();
echo $xx->ff(); // prints 4

In the above, even though “B1” doesn't have any members, but it inherited members from class A1.

If a subclass defines a method that exists in parent class, it'll be overwritten.

Any method declared with final keyword cannot be overwritten.

Constants: “const”

A “constant” is like a variable except that its value cannot be changed. It's used, for example, to define the value of math π.

Use keyword const to declare a constant in a class.

Use self::constant_name to access the constant inside the class.

<?php
class AA
{
  const cc = 3; // defines a constant

  public function ff() {
    // use self::constant_name to access a constant inside a class
    return self::cc + 1;
  }
}

// constant can be accessed by ::, no need to create a object
echo AA::cc, "\n"; // prints 3

$xx = new AA();
echo $xx->ff(); // prints 4

Static Properties and Static Methods

A class property can be declared with the keyword static.

A class method can be declared with the keyword static.

When a property or method is declared static, their value is shared across all instance of the class.

{self, parent} for Accessing Static Properties or Methods

The keywords {self, parent} can used to access {static properties, static methods, constants}, from inside the class definition. The syntax is like this: self::$static_proper_name

<?php
class AA
{
  const a1 = 3; // defines a constant
  public function ff() { return self::a1; }
}

$xx = new AA();
echo $xx->ff(); // prints 3