OO PHP
IT Notes → PHP @ December 22, 2020
- Predominantly version 5.
-
class cname { access control modifier $variable; //Known as class property. access control modifier function(){} //Known as class method. } $obj = new cname; //Or $obj = new cname(); $obj->methodname();
- $this variable always points to the object you are currently working with.
- Access control modifiers: public, private, protected, abstract and final.
- If there is an abstract method the class should be abstract.
- instanceof and is_subclass_of( ) =>$poppy instanceof Poodle and is_subclass_of($poppy, “Poodle”);
- classes and interface can be given as hints.
- extends keyword for inheritance.
- __construct() =>Constructor parent should be created initially in the child.
- __destruct() => Destructor parent should be created last from the child.
- parent::.
- unset() to destroy an object => will call destructor.
- clone obj to clone an object.
- In cloning all members will be copied and then __clone() method will be called and it can be overridden.
- == Compares equality of value whereas for === the handler should be same. Problem is if we use == in object comparison then a loop can occur as the object may be self referenced and it searches deeply again and again to get the value for comparison.
- Polymorphism is defined as one interface to control access to a general class of actions. There are two types of polymorphism one is compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is functions and operators overloading. Runtime time polymorphism is done using inheritance and virtual functions.
- Early binding determines execution path at compilation and late binding allows for dynamic execution at runtime.
- Magic methods:
- __function().
- __sleep() for serialize() and __wakeup() for deserialize.
- __autoload(classname) =>When an object declared prior to definition this function gets called with classname. We can used to include it.
- __get(propname) => If unknown property name is used then this function is called.
- __set(key, value) =>If unknown property is set key and value will be send to this function.
- __call() => If unknown method called then method name passed to this.
- __toString() => If class is used as a string, then this function is invoked.
- Static method calls are compiled at compile time and is accessed using classname::fnname [self]. Static properties can’t be accessed outside static functions.
- class_exists( ), get_class( ), and get_declared_classes( ).
- Overloading is defining functions that have similar signatures, yet have different parameters. Overloading in PHP means to dynamically create properties and methods. They can be processed using magic functions like __call to create various action types.
- Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that function.
- A class is made abstract if it has abstract function both of which require abstract keyword prefixed. They are not defined in the parent class and has been defined in an inherited These methods must be defined with the same (or a less restricted) visibility.
- Interface helps in implementing multiple inheritance. Interface is defined using interface keyword and implemented using implements keyword. Also, all methods must be implemented if interface is implemented. It is a contract to implement all methods.
- Dereferencing means accessing the return refence without first assigning it to an intermediate variable and then accessing through the intermediate variable. Return value dereferencing is allowed only for objects funcreturningobject()->member.
- Lazy loading means loading classes on demand. the PHP interpreter calls __autoload(class) function which can be used to load the class by say including or requiring a path using the class name. SPL additionally provides us a facility to stack autoloaders where next tries to load fails if previous fails. So different schemas like classes in different paths can be loaded.
- Trait allows reuse of code fragments horizontally across classes. We define it using keyword trait to group functions and keyword use is use to substitute it in classes.
- Reflection is an APi set that can reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.
Subscribe
Login
0 Comments