Reading Note 4 – PHP 5 Advanced OOP and Design Patterns

phpoo

OK. It’s Object Oriented Time!

1. Because a constructor cannot return a value, the most common practice for raising an error from within the constructor is by throwing an exception.

2. $obj = NULL will destruct the obj (or __desctruct() ill be called.

3. Static properties are defined by using static keywords, and you can think it as global variable that sit inside the class, but are accessible from anywhere via the class. Static property is used in a singleton pattern.

4. Static properties or methods can be called outside the class without creating an instance.

class{
static staticproperty;
}
classname::staticproperty;
classname::staticmehtod();

$this->staticproperty //THIS IS WRONG!!!!

5. parent:: and self::
They can access to static members, methods (both static and non-static method), and constants.

class A{
public $abc = 2;
function getabc(){
echo self::abc; //THIS IS WRONG, because abc is not static
}
}
Class B extends A{
function getabc(){
echo parent::abc; //HIS IS WRONG, because abc is not static
}
}

6. public protected, private are access modifiers for both properties and menthods

7.class constants do not use the define() function, which is used for global constant, but use the “const” keyword.

class myclass {
const RD = "red";
}

8. polymorphism uses “extends” keyword and means inheritance

9. PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and 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.

10. In PHP, you can only extends from one parent class, but you can use implements to archive multiple inheritance.

class A implements B, C, ... {
...
}

11. interface class like abstract. So no implantation of method and can not be directly instantiated. The different is that you can only extend from one abstract, but can extend from multiple interfaces.

12. final methods.
If you want to make sure that a method cannot be re-implemented in its derived classes, then use final access modifier.

13. final classes.
Final classes are not allowed inherited.

12. classes cannot be defined as abstract, final, but all classes are public in PHP. There’s no such thing as a “private/protect class”.

13. Exceptions
When using exceptions, follow these basic rules (both for performance and code-manageability reasons):

  1. 1. Remember that exceptions are exceptions. You should only use them to handle problems, which brings us to the next rule….
  2. 2. Never use exceptions for flow control. This makes the code hard to follow (similar to the goto statement found in some languages) and is slow.
  3. 3. The exception should only contain the error information and shouldn’t contain parameters (or additional information) that affect flow control and logic inside the catch handler.

14. Example to catch dived by zero exception
PHP5 brought exceptions to the table, and newer PHP provided libraries (PDO) will throw exceptions when bad/unexpected things happen. Hoever, the core codebase was NOT rewritten to use exception. Core functions and operations still rely on the old error system. If you want to “catch” these, you’ll need to set a custom error handler that will detect division by zero errors and do something about them.

try{
   //$a = 1/0;
   $a = devide(1,0);
   echo $a;
}
catch(DevidedByZeroException $e1){
   echo $e1->getMessage();
   print " in file " . $e1->getFile();
   print " on line " . $e1->getLine() . "\n";
}
catch(Exception $e){
   echo $e->getMessage();
}


function devide($a, $b){
   if($b==0){
      throw new DevidedByZeroException("Division by zero is invalid");
   }
   else{
      return $a/$b;
   }
}

class DevidedByZeroException extends Exception {
   function __construct($message)
   {
      parent::__construct($message);
   }
}

14. __autoload()

function __autoload($class_name) {
include $class_name . '.php';
}

$obj = new MyClass1();
$obj2 = new MyClass2();

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.


// function __autoload($class) {
// include 'classes/' . $class . '.class.php';
// }

function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}

//as php5.1.2
// you can have multiple callback functions
spl_autoload_register('my_autoloader');

// Or, using an anonymous function as of PHP 5.3.0
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});

Leave a Reply

Your email address will not be published. Required fields are marked *