OOPS CONCEPTS IN PHP
9 August 2008THE BASICS
CLASS
- Every class definition begins with the keyword class, followed by a class name, which can be any name that isn’t a reserved word in PHP.
- Followed by a pair of curly braces, which contains the definition of the classes members and methods.
- A pseudo-variable, $this is available when a method is called from within an object context.
- $this is a reference to the calling object (usually the object to which the method belongs, but can be another object, if the method is called statically from the context of a secondary object).
NEW
· To create an instance of a class, a new object must be created and assigned to a variable. An object will always be assigned when creating a new object unless the object has a constructor defined that throws an exception on error.
· Classes should be defined before instantiation (and in some cases this is a requirement).
Example Creating an instance
<?php
$instance = new SimpleClass();
?>
· In the class context, it is possible to create a new object by new self and new parent.
· When assigning an already created instance of a class to a new variable, the new variable will access the same instance as the object that was assigned.
· This behaviour is the same when passing instances to a function. A copy of an already created object can be made by cloning it.
Extends
· A class can inherit methods and members of another class by using the extends keyword in the declaration.
· It is not possible to extend multiple classes, a class can only inherit one base class.
· The inherited methods and members can be overridden, unless the parent class has defined a method as final, by redeclaring them with the same name defined in the parent class.
· It is possible to access the overridden methods or static members by referencing them with parent::
AUTOLOADING OBJECTS:
· Many developers writing object-oriented applications create one PHP source file per-class definition.
· One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
· In PHP 5, this is no longer necessary.
· You may define an __autoload function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet.
· By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
CONSTRUCTORS AND DESTRUCTORS:
Constructor:
void __construct ([ mixed $args [, $... ]] )
· PHP 5 allows developers to declare constructor methods for classes.
· Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.
· For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, it will search for the old-style constructor function, by the name of the class.
· Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics.
Destructor:
void __destruct ( void )
· PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++.
· The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence.
VISIBILITY:
· The visibility of a property or method can be defined by prefixing the declaration with the keywords: public, protected or private.
· Public declared items can be accessed everywhere.
· Protected limits access to inherited and parent classes (and to the class that defines the item).
· Private limits visibility only to the class that defines the item.
Members Visibility:
Class members must be defined with public, private, or protected.
Method Visibility:
· Class methods must be defined with public, private, or protected. Methods without any declaration are defined as public.
· The Scope Resolution Operator or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden members or methods of a class.
· When referencing these items from outside the class definition, use the name of the class.
· As of PHP 5.3.0, it’s possible to reference the class using a variable. The variable’s value can not be a keyword (e.g. self, parent and static).
STATIC KEYWORD:
· Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
· For compatibility with PHP 4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public.
· Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static.
· Static properties cannot be accessed through the object using the arrow operator ->.
CLASS CONSTANTS:
· It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don’t use the $ symbol to declare or use them.
· The value must be a constant expression, not (for example) a variable, a class member, result of a mathematical operation or a function call.
· As of PHP 5.3.0, it’s possible to reference the class using a variable. The variable’s value can not be a keyword (e.g. self, parent and static).
CLASS ABSTRACTION:
· PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature they cannot define the implementation.
· When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.
OBJECT INTERFACES:
· Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
· Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
· All methods declared in an interface must be public, this is the nature of an interface.
implements
· To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
Note: A class cannot implement two interfaces that share function names, since it would cause ambiguity.
TYPE OPERATORS:
· instanceof is used to determine whether a PHP variable is an instantiated object of a certain class:
OVERLOADING:
· Overloading in PHP provides means to dynamically “create” members and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.
· The overloading methods are invoked when interacting with members or methods that have not been declared or are not visible in the current scope. The rest of this section will use the terms “inaccessible members” and “inaccessible methods” to refer to this combination of declaration and visibility.
· All overloading methods must be defined as public.
Note: None of the arguments of these magic methods can be passed by reference.
Note: PHP’s interpretation of “overloading” is different than most object oriented languages. Overloading traditionally provides the ability to have multiple methods with the same name but different quantities and types of arguments.
OBJECT ITERATION:
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
PATTERNS:
Patterns are ways to describe best practices and good designs. They show a flexible solution to common programming problems.
MAGIC METHODS:
The function names __construct, __destruct (see Constructors and Destructors), __call, __callStatic, __get, __set, __isset, __unset (see Overloading), __sleep, __wakeup, __toString, __set_state and __clone are magical in PHP classes
PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.
FINAL KEYWORD:
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
COMPARING OBJECTS:
· In PHP 5, object comparison is more complicated than in PHP 4 and more in accordance to what one will expect from an Object Oriented Language (not that PHP 5 is such a language).
· When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.
· On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
· In PHP 5, object comparison is more complicated than in PHP 4 and more in accordance to what one will expect from an Object Oriented Language (not that PHP 5 is such a language).
· When using the comparison operator (==), object variables are compared in a simple manner, namely: Two object instances are equal if they have the same attributes and values, and are instances of the same class.
· On the other hand, when using the identity operator (===), object variables are identical if and only if they refer to the same instance of the same class.
REFLECTION:
Table of Contents:
- Introduction
- The Reflector interface
- The ReflectionException class
- The ReflectionFunction class
- The ReflectionParameter class
- The ReflectionClass class
- The ReflectionObject class
- The ReflectionMethod class
- The ReflectionProperty class
- The ReflectionExtension class
- Extending the reflection classes
Introduction:
PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions and methods as well as extensions. Additionally, the reflection API also offers ways of retrieving doc comments for functions, classes and methods.
Reflector:
Reflector is an interface implemented by all exportable Reflection classes.
ReflectionException:
ReflectionException extends the standard Exception and is thrown by Reflection API. No specific methods or properties are introduced.
ReflectionFunction:
The ReflectionFunction class lets you reverse-engineer functions.
ReflectionParameter:
The ReflectionParameter class retrieves information about a function’s or method’s parameters.
ReflectionClass:
The ReflectionClass class lets you reverse-engineer classes and interfaces.
ReflectionObject:
The ReflectionObject class lets you reverse-engineer objects.
ReflectionProperty:
The ReflectionProperty class lets you reverse-engineer class properties.
ReflectionExtension:
The ReflectionExtension class lets you reverse-engineer extensions. You can retrieve all loaded extensions at runtime using the get_loaded_extensions().
TYPE HINTING:
PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.
- Create Learning With a Document Camera
- A Great View From Everywhere!
- Picture This!- Helping Students with Autism Learn
Leave a reply
You must be logged in to post a comment.

