Summary: Practical OOP for PHP 5.2

Declaring a Class:

class ClassName {
  <properties>
  <methods>
}

Store in:

classname.php

What to name the constructor:

__construct
$this

special variable

Magic methods

start with two underscores (__)

Destructor

__destruct

Has no arguments; and, there are no guarantees about when it will run.

Instantiate an object

require 'newclass.php';

$var = new NewClass(..parameters);
var_dump()

is useful

Autoloading

Autoloading is when we tell PHP where to look for our class files when it needs a class declaration.

Define:

__autoload()

Autoloading is only useful if I name and store the class files in a predictable way.

Read more…

Practically I’ll need to include this __autoload function definition in all my scripts.

Using Objects

$var->property

$var->method(..parameters)

Static Properties and Methods

static

A static method or property is one that gets used without instantiating the object.

When declaring something static put the static keyword after the visibility keyword.

::

is the operator

A static property belongs to the class (not to an object.)

A static method has no need to access any other part of the class.

Static properties were sometimes used for namespacing.

Static methods get used to retrieve collections of objects.

Must declare any method which will be called statically as static.

Namespaces for Objects

PHP 5.3 or above (I use 5.2.) Namespaces are for isolating classes and functions of the same name in different libraries.

Inheritance

Keyword:

extends

A child class has all the characteristics of its parent class, and we can add or change any elements that need to be different for the child.

class Jerk extends Jackass {
  class-definition..
}

For a class to truly redeclare a method, it must use the same parameter list.

Passing objects to functions and methods

Type Hinting

Type hinting is when I declare the type of a parameter in a function’s parameter list.

I can type hint using a class name or the keyword array. However, I cna’t type hint with simpler data types.

Polymorphism

Polymorphism is when there is type hinting and PHP accepts a particular parameter which is a class that is a descendant of the declared class. Or more generally speaking any time a descendant is accepted where the ancestor was the one that was expected.

instanceOf

is an operator. It can be used for figuring out if a variable is an instance of a class or interface (more on interfaces later.)

Objects as References

Once an object variable is created there will only be one copy of that object. It doesn’t matter if you assign it or pass it as a parameter. However, you can clone:

$newVar = clone $var;

// magic method which specifies how the cloning will be done:
__clone

Fluent Interfaces

To make this work each function has to return $this.

That is when you chain methods together. It works from left to right.

Visibility

public—outside and iside own class, private—inside own class, protected—inside all relatives.

Getters and Setters

Usefulness

  • traceability
  • provide hooks

__get and __set allow for the simulation of class properties which don’t exist. Read more..

Interfaces

They can be used to declare the features of a class or function. They can even re-define PHP built-in functions. You can declare an interface, specify an interface is being implemented, or both.

SPL

Standard PHP Library

Countable is an example.

SPL is a module for PHP which provides:

  • interfaces
  • iterator classes
  • storage classes

If there is a particular interface for a particular function; and, you declare a method in your class to be part of that interface. Then, that method becomes a global function which overrides any function of the same name.

instanceOf works for interfaces too.

Exceptions

Exceptions fall under the subject matter of OOP; but, I won’t say anything about them here.

__toString

Is used to specify how echo will work on an object.

__sleep() and __wakeup()

These specify what happens when the object is acted upon by serialize or unserialize.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in PHP Object Oriented Programming. Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s