OOP concepts in PHP 5 in short Why this short – note? if you are familiar with OOD and any OOP language such as Java/C++, this short note will give you enough information to start with PHP 5 OOP Class
- Class definition starts with the keyword class, followed by a class name (non reserved word), followed by a pair of curly braces. The curly braces contain the definition of the classes members and methods
- You can create objects based on the classes. $obj = new className()
- You use $obj->methodName() to access a class method (public). You can use className::classMember to access class members (static): to use :: operator the method does not need to be declared static
- Inside a class all class methods have access to $this variable to refer to the calling object (if called from/using an object)
- member declaration: public $var = ‘a default value
- Default value always is: constant expression
- Class/Object Functions
- A class can use extends keyword to inherit methods and members of another class
- Multiple inheritance is not allowed
- To avoid using a long list of includes in the beginning of php files, you can use __autoload() function to do the job for you
- When you try to use an undefined class/interface an __autoload function is automatically called
-
function __autoload($class_name) { require_once $class_name . '.php'; }
- Constructor syntax: void __construct ([ mixed $args [, $… ]] )
- Parents’ constructors are not automatically called from children’s constructos. use explicit parent::__construct() instead
- Destructor syntax: void __destruct ( void )
- Destructor is called: 1. all references to the object are removed 2. the object is explicitly destroyed 3. in shutdown sequence
- Parents’ destructors are not automatically called from children’s destructors. use explicit parent::__destruct() instead
- Access modifiers for class members: public, protected or private: Public – accessible from anywhere. Protected – accessible from inherited and parent classes, within the class. Private – accessible within the class
- No access modifier = public
- :: – scope resolution operator – allows access to static, constant, and overridden members or methods of a class
- Abstract classes: Introduced in PHP 5. You are not allowed to reate an instance of an abstract class.
- Even if a class contains one abstract method, the class must bedeclared abstract
- Abstract classes are just about signatures, they cannot define the implementation
- A class inheriting from an abstract class, must have to implement all abstract methods. The abstract methods must be defined with the same/(less restricted) visibility
- Interface: Just the method signatures. No method implementation inside interfaces
- All interface methods must be public
- Classes implementing interfaces must implement all methods. Classes use implements keyword to implement an interface
- A class can not implement two interfaces having same class names
- Interfaces can be extended using extends keyword
- Interfaces can also have constants
- Overloading: Overloading in PHP = dynamically “create” members and methods
- overloading methods: invoked when interacting with non-declared/invisible members or methods
- All overloading methods must be defined as public
- In PHP, overloading is done through magic methods
- The arguments of the magic methods can not be ‘passed by reference’
- Member overloading methods: void __set ( string $name , mixed $value ), mixed __get ( string $name ), bool __isset ( string $name ), void __unset ( string $name )
- Method overloading: mixed __call ( string $name , array $arguments ), mixed __callStatic ( string $name , array $arguments )
- Object Iteration: Inside the class
foreach($this as $key => $value) { print "$key => $valuen"; }
- Object Iteration: Outside class:
$class = new MyClass(); foreach ($class as $key => $value) { print "$key => $valuen"; }
- Patterns: Factory Pattern: allows the instantiation of objects at runtime
- Patterns: Singleton: Helps in situations where only a single instance of a class is required that will be used by many other objects
- Magic methods: have special meaning. __construct, __destruct (see Constructors and Destructors), __call, __callStatic, __get, __set, __isset, __unset, __sleep, __wakeup, __toString, __set_state and __clone
- serialize() – applies to __sleep(). unserialize() applies to __wakeup()
- final keyword: final members can not be overriden, final classes can not be extended
- $copy_of_object = clone $object; : will create a clone of $object. Unless a __clone method defined, a shadow is created. __clone() method can define how the cloning will be done
- Objects Comparison: == : two object instances are equal if they have the same attributes and values, and are instances of the same class.
- Objects Comparison: === : Object variables are identical if and only if they refer to the same instance of the same class
- Reflection APIs: to reverse-engineer classes, interfaces, functions and methods, extensions
- Reflection APIs: Offer ways to retrieve doc comments for functions, classes and methods
- Type Hinting: Functions can enforce parameters to be objects:
- Late Static Bindings: to refer the called class in a context of static inheritance.
From: http://sitestree.com/?p=5105
Categories:16
Tags:
Post Data:2011-07-30 15:39:51
Shop Online: <a href='https://www.ShopForSoul.com/' target='new' rel="noopener">https://www.ShopForSoul.com/</a>
(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http://Training.SitesTree.com' target='new' rel="noopener"> http://Training.SitesTree.com</a>
In Bengali: <a href='http://Bangla.SaLearningSchool.com' target='new' rel="noopener">http://Bangla.SaLearningSchool.com</a>
<a href='http://SitesTree.com' target='new' rel="noopener">http://SitesTree.com</a>
8112223 Canada Inc./JustEtc: <a href='http://JustEtc.net' target='new' rel="noopener">http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning) </a>
Shop Online: <a href='https://www.ShopForSoul.com'> https://www.ShopForSoul.com/</a>
Medium: <a href='https://medium.com/@SayedAhmedCanada' target='new' rel="noopener"> https://medium.com/@SayedAhmedCanada </a>