Please go through this php svn for a better understanding of php coding standard or naming convention.
To define a Class “Class” keyword is used. A class name could be anything which is not a PHP reserved word. A valid class name starts with a letter or an underscore, followed by any number of letters, numbers, or underscores. Please note that a Class is only a template. It is meaningless without an Instance (Object) of that Class. An Instance allocates memory for a Class and keeps the reference itself.
Class A { // properties // methods }
A class normally contains both properties and methods. There are no rules about the order in which they should appear inside the class, but the normal convention is to declare all the properties first, followed by the methods.
Class A { Public $property1=1; Private $property2="Hello World"; Protected $property3="10.3 m/s"; Public function function1(){ ------------------ } Protected function function2(){ --------------------- } Private function function3(){ ------------------ } }
Visibility or Accessibility: The simplest meaning to this is up to which Class we can access these methods or properties.
When we have only one Class called Class A then these keywords namely private public and protected has no meaning. All are same for Class A. These will be only meaningful when some Class B inherit Class A.
Class B extends Class A { //some properties //some functions }
And for our convenience we will define a third class Class C.
Class C { //some properties //some methods }
- Public: Class properties or methods declared as public can be accessed by all the Classes.
- Protected: Members declared as protected can be accessed only within the class itself and by inherited and parent classes.
- Private: Members declared as private may only be accessed by the class that defines the member.
Now from the above written visibility rule we can conclude that:
- Class A can access all properties and methods defined within the Class it has no restriction.
- Class B can access public as well as protected methods and properties.
- Class C can access only the public methods and properties nothing else.
One might think like what to make private and what to make public and what to make protected. As a general rule whenever you come to a situation where you need to use some secret data such as Database Name/Password or email_id/password make them private. When one module is consists of several Classes and will be used within that module than use methods Protected within the scope of module (for example an email module could consists of several classes one for smtp or for mime support and like that) and otherwise if you are making stand alone class make everything public.
“New” keyword is used to create an Instance of the Class. Thus,
$instance = new Class A();
will create an object/Instance of Class A. and to execute methods within Class A we will use the Instance as,
$instance->funtion1();
Please note the syntax of creating an Instance: new than Class Name than small brackets. It is the only syntax for creating an instance of the Class till 5.2. As of 5.3 there are several ways to make an instance of a Class. We will read more about that later.
$var1 = & $instance;
This statement assigns the same reference to $var1. This behavior is called cloning. And
$instance->function1(); or $var1->function1();
will be same results.
Constructor/Destructor Function: As the name suggests a constructor create a Class and a Destructor destruct a Class. It is better to instantiate any properties within the Class before using them. Note that properties defined within the Class for use with methods takes arguments defined within the Class.
When we instantiate using “new” keyword all the memory blocks needed by the Object of the Class is allocated. So it is always a good Idea to instantiate with all necessary data at the time of creating instance of that Class.
The syntax of a constructor is something like this
function __construct ($args ... ){ //property assignment; }
Note: A constructer should not return anything.
You can use a constructor just for property assignment or some other thing needed prior to execute the methods of that Class.
Note: There are no rules about the order in which a Constructor or Destructor should appear inside the class, but the normal convention is to declare Constructor first, followed by its methods than Destructor at last.
A Destructor frees memory allocated for the Object and all references to that particular object. A destructor should not have a return type similar to Constructor plus should not have an argument. The syntax of Destructor is something like this:
function __destruct ( void ){ }
Generally a destructor has no statement relevant to the Class. But a Programmer is free to test the Proper Destruction using some sort print statements.
Class Constants: As the name suggests it is a constant for the Class not for an Object. It is possible to modify or make some sort of manipulation on them in some Object of that Class or outside but it will be same for all the objects of that Class. Meaning is it is directly associated with the Class and not with the Object. That means when we create an Instance using “new” keyword for that Class no memory will be created for that constant. That also means that constants are defined at compile time not at runtime. So anything that will need a runtime assignment should not be used as a value for the constant. Like array and functions. Take a look at the following example.
Note: array is a data structure which is only possible at runtime not at compile time.
Note: Constants differ from normal variables in that you don’t use the $ symbol to declare or use them.
Note: Note that since constants are tied to the class definition, they are static by definition and cannot be accessed using the -> operator.
The syntax for declaring a constant is as follows:
const constant = 'constant value'; // const is a keyword. class MyClass { const constant = 'constant value'; function showConstant() { echo self::constant ; } } echo MyClass::constant;
Note: As of PHP 5.3.0, it’s possible to reference the class using a variable provided value within the variable should not be a reserved keyword.
As,
$v="MyClass"; $v::constant; //will output the same result.
Static Property: A static property or method is sometimes referred to as a class property or class method in the sense that it belongs to the whole class and not to an individual instance of the class and accessible without needing an instantiation of the class. 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. Like any other PHP static variable, static properties may only be initialized using a literal (basic data type) or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object. That means you can’t initialize a static variable more than once.
To make a property or method static, we use the keyword static after the visibility modifier. If no visibility declaration is used, then the property or method will be treated as if it was declared as public.
Private static $my_static = 10;
In some respects, a static property acts very much like a class constant, but unlike a constant, you can restrict its scope by using the protected or private keywords.
Inheritance: By means of inheritance a subclass acquires the properties of a base class. When we extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a subclass overrides parent/base Class’s methods, they will retain their original functionality.
Note: If a class extends another, then the parent class must be declared before the child class structure. This rule applies to class that inherit other classes and interfaces.
Class A { function print_one() { echo "one"; } } Class B extends Class A{ Function print_one() { echo "Changed"; } } $obj1 = new Class A(); $obj2 = new Class B(); $obj1->print_one(); //Output: 'one' $obj->2print_one(); //Output: 'Changed' //because Class B override the method of Class A.
Note: PHP do support implicit $this pointer. The this pointer holds the address of the class object for which the member function is called.
Two special keywords self and parent are used to access properties or methods from inside the class definition.
Scope Resolution Operator (::): The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class. When referencing from outside the class definition, use the name of the class.
Example 1: from inside the class definition
class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE ; echo self::$my_static ; } }
Example 2: Calling a parent’s method
class MyClass { protected function myFunc() { echo "MyClass::myFunc()"; } } class OtherClass extends MyClass { // Override parent's definition public function myFunc() { // But still call the parent function parent::myFunc(); echo "OtherClass::myFunc(); } }
Note: please note that 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.
Here is a diagram of what ever we have learned so far.
Abstract Class: PHP also supports the use of an abstract class. An abstract class is just meant to be inherited by other Class and can’t run itself. To better understand abstract we must know what is a abstract method.
An abstract method does not have an implementation; that is, no method body is defined for an abstract method, only the method prototype is provided in the Class definition. A real life situation would be something like this; A Class Time could have a method calculate_time. But its Subclasses IST and UTC has different time calculation implementations. So calculate_time should be defined as abstract.
Any class that contains at least one abstract method must also be abstract and Classes defined as abstract may not be instantiated; that is, we can’t create an object of an Abstract Class.
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.
abstract class AbstractClass { // Force Extending class to define this method abstract protected function getValue(); abstract protected function prefixValue($prefix); // Common method public function printOut() { print $this->getValue() . "\n"; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { public function getValue() { return "ConcreteClass2"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass2"; } } $class1 = new ConcreteClass1; $class1->printOut(); //Output: ConcreteClass1 echo $class1->prefixValue('FOO_') ; //Output: FOO_ConcreteClass1 $class2 = new ConcreteClass2; $class2->printOut(); // Output: ConcreteClass2 echo $class2->prefixValue('FOO_') ; //Output: FOO_ConcreteClass2
we can’t instantiate an Abstract Class but we can invoke a static member of method using the Class name.
abstract class Foo { static function bar() { echo "test\n"; } } Foo::bar();
Interfaces: An interface is very similar to an abstract class, but it has no properties and cannot define
how methods are to be implemented. Instead, it is simply a list of methods that must be
implemented. Moreover, all methods must be public. It has two main purposes:
- An interface ensures that all classes that implement it have a common set of functionality.
- Although a child class can have only one parent, it can implement multiple interfaces.
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. 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.
// Declare the interface interface Super { public function setVariable($name, $var); public function getHtml($template); } // Implement the interface class Template implements Super { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } }
Classes may implement more than one interface if desired by separating each interface with a comma. As
interface A { function a(); } interface B { function b(); } class C implements A, B { function a() { echo "a\n"; } function b() { echo "b\n"; } }
Note: Interfaces can be extended like classes using the “extends” operator.
Note: A class cannot implement two interfaces that share function names, since it would cause ambiguity.
Note:The class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error.
Note: Its possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits it.
It is natural for a reader to feel some sort of difficulties why this interface is used when we have abstract Class. To understand the usage of interface and abstract we must create some situation when abstract class simply won’t work. Let us we have these objects.
Person
Employee
Employer
Criminal
Rapist
President
Student
From the above Object set we can understand.
1. An Employee is a person.
2. An Employer is a person.
3. A Criminal is a person and so on.
You have two or more objects that share similar behavior. So it is feasible to define a Person Class as an Abstract Class because all Persons have same behavior. Now when we come to individual classes we could have several methods for the Classes. For e.g. President and Employer will makePolicy() while Criminal and Rapist will commitCrime() having said that, normally people would put these methods in their respective classes but defeating the OO designs. In OO design we should not duplicate our methods. If you put makePolicy() in President and Employer class there will be duplication of code. Other people can be tempted to put thet makePolicy() and commitCrime() in Person as abstract methods so that the 6 objects will see the methods. Its not a good idea as Student or Employee will not normally makePolicy() or commitCrime(). In fact, it means Employee object can makePolicy() of increasind salary. . Therefore in this case we use INTERFACES
abstract Class Person{ public $age; public $location; abstract public function getAge(); } interface HighOffice { public function makePolicy(); public function declareEmergency(); } interface jailable { public function commitCrime(); public function appeal(); } Class Employee extends Person { } Class Criminal extends Person implements jailable{ public function commitCrime(){ } public function appeal(){ } } Class President extends Person implements HighOffice { public function makePolicy(){ } public function declareEmergency(){ } }
Final keyword: There might be some situation when it is required that a subclass should not override a method in the base class. A Final keyword 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.
Example #1 Final methods example
class BaseClass { public function test() { echo "BaseClass::test() called\n"; } final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; } } class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called\n"; } } // Results in Fatal error: Cannot override final method BaseClass::moreTesting()
Example #2 Final class example
final class BaseClass { public function test() { echo "BaseClass::test() called\n"; } // Here it doesn't matter if you specify the function as final or not final public function moreTesting() { echo "BaseClass::moreTesting() called\n"; } } class ChildClass extends BaseClass { } // Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
Note: Properties cannot be declared final, only classes and methods may be declared as final.
Note: A user might think of what is the difference between a private member function and methods having final keyword. The difference is final provide restriction so that a derived class can’t override its method or in case of a Class restrict itself form being inherited.
Note: The final keyword doesn’t change the visibility of a property / method, which is public by default.