PHP OOP Concept - Advance features

[This note is taken when reading “PHP Objects, Patterns and Practice, 2nd edition“]

++ Static methods and properties: Accessing data and funtionality through classes rather than objects. Static methods are functions with class scope. They only can access to static properties, not to other normal properties. If you change a static property, all instances of that class are able to access the new value.

- Class code can use the parent keyword to access a superclass without using its class name. We use :: in conjunction with parent to access an overridden method. To access a static method or property from within the same class, we would use the seft keyword. seft is to classes what the $this pseudo-variable is to object.

-Static method are not invoked in the context of an object (cannot use $this inside a static method)

++ Constant properties can not be changed once they are set. Constants are not prefix with a dollar sign and can contain only primary values. By convention, they are often named by using only uppercase characters. Keyword: const. Constant properties can be accessed via the class not an instance.

++ An abstract class can not be instantatiated. Instead it defines (and, optionally, partial implements) the interface for any class that might extend it. In most case, an abstract class will contain at least one abstract method. Abstract class is declared with abstract keyword, and ended with a semicolon rather than a method body. Any class that extends an abstract class must implement all abstract methods or itself be declared abstract.

++ While abstract classes let you provide some measure of implementation, interfaces are pure templates. An interface can only define functionality; it’s never implement it. It can contain properties and method declarations, but not method bodies.

- A class can implement an interface by using implements keyword and it must implement all methods, which the interface defines, or be declared as an abstract class.

- A class can extend a superclass and implement any number of interfaces.

- To know an object’s type is to know it capabilities. That’s a benefit of implementing an interface.

++ Handling errors: we can use or extend Exception class to handle errors using try… catch, and throw method. With a try expression, we can have more than one catch expression with different types of Exception. This allows you to apply different recovery or failure mechanisms to different errors: You may decide to end execution, log the error and continue, or explicitly rethrow the error (give responsibility for handle error to client code when your method has detected an error but does not have contextual information to handle it intelligently).

++ When you have achieved the definitive functionality for your class or method, and you feel that overriding it can only damage the ultimate perfection of your work, you may need to change it to final class or method. final puts a stop to inheritance. A final class cannot be subclassed. A final method cannot be overridden.

++ PHP provides built-in interceptor method, which can intercept messages sent to undefined methods and properties ( __get($property); __set($property, $value); __isset($property); __unset($property); __call($method, $arg_array); ). The __call method() can be useful for delegation. Delegation is the mechanism by which one object passes method invocations on to a second (Ex: using __call() to pass an indefined method(writeName) in Class to the same method(writeName) in Class_Writer)

++ Destructor method is invoked just before an object is garbage-collected; that is, before it is expunged from memory (__destruct(){})

++ Clone an object: clone keyword operates on an object instance, producing a value-copy object. When clone is invoked on an Object, a new shallow copy is made, and its __clone() method is invoked (so anything we do in __clone() method will overwrite the default copy we already made). A shallow copy ensures that primary values are copied from the old object to the new. Object properties, though, are copied by reference; so if you don’t want a Object property will be shared after clone operation, then it is up to you to clone it explicitly in the __clone() method ($this->account = clone $this->account).

++ Defining a string value for your method by implementing __toString() method. This method is invoked automatically when your object is passed to echo or print and it controls how your objects represent themselves when printed.

This was posted 2 years ago. It has 0 notes.