Introduction to PHP 5

Introduction to PHP 5

In that article we’ll talk about innovations in PHP 5:

  • New object model
  • Exceptions
  • Namespaces

New object model

Object model is modified in PHP 5 and has a lot of new opportunities. In that part of our article we’ll talk about new object model and will review come examples.

  • Constructors and destructors
  • Objects as links
  • Object cloning
  • Descriptors Private, Public and Protected
  • Interfaces
  • Abstract classes
  • _call
  • _set and _get
  • Closed terms

Constructors and destructors

In PHP 4 constructor is named as a class, and there are no destructors at all. In PHP 5 constructor is named as _construct, destructor named as _destruct.

For example:

<?php 
class foo 
  var 
$x

  function 
__construct($x) { 
    
$this->$x
  } 

  function 
display() { 
    print(
$this->x); 
  } 

  function 
__destruct() { 
    print(
"Good bye"); 
  } 


$o1 = new foo(4); 
$o1->display(); 
?>  

As you can see destructor is called before class destruction.

Objects as links

In PHP 4 variables are transferred to the functions/methods by value, if there isn’t set '&' symbol. In PHP 5 objects are always transferred as links.

For example:

<?php 
class foo 
  var 
$x

  function 
setX($x) { 
    
$this->$x
  } 

  function 
getX() { 
    return 
$this->x
  } 


$o1 = new foo
$o1->setX(4); 
$o2 $o1
$o1->setX(5); 
if(
$o1->getX() == $o2->getX()) print("oh, my God"); 
?>  

Object cloning

If objects are transferred by link copies of that objects should be created. It can be done with the help of _clone function.

For example:

<?php 
class foo 
  var 
$x

  function 
setX($x) { 
    
$this->$x
  } 

  function 
getX() { 
    return 
$this->x
  } 


$o1 = new foo
$o1->setX(4); 
$o2 $o1->__clone(); 
$o1->setX(5); 

if(
$o1->getX() != $o2->getX()) print("Copy"); 
?>  

Descriptors Private, Public and Protected

In PHP 4 all methods and variables inside the object were accessible from the outside. In PHP 5 there are three descriptors used for controlling access to variables and methods. They are Public, Protected and Private.

  • Public: method or variable are accessible from any place in the code
  • Private: private methods and variables are accessible only inside the class
  • Protected: protected methods and variables are accessible inside the class they were notified and inside the derivative classes.

For example:

<?php 
class foo 
  private 
$x

  public function 
public_foo() { 
    print(
"It is public method"); 
  } 

  protected function 
protected_foo() { 
    
$this->private_foo(); 
    print(
"It is protected method"); 
  } 

  private function 
private_foo() { 
    
$this->3
    print(
"It is private method"); 
  } 


class 
foo2 extends foo 
  public function 
display() { 
    
$this->protected_foo(); 
    
$this->public_foo(); 
    
// $this->private_foo();  
  



$x = new foo(); 
$x->public_foo(); 
//$x->protected_foo();  
//$x->private_foo();      

$x2 = new foo2(); 
$x2->display(); 
?>

Class variables always should be private, because direct access is enough dangerous.

Interfaces

As you know PHP 4 supports class inheritance with syntax "class foo extends parent". In PHP 5 class can inherit only one class, i.e. multiple inheritance isn’t supported. Interface is a class where none implemented, where method name and set of its parameters are used.

<?php 
interface displayable 
  function 
display(); 


interface 
printable 
  function 
doprint(); 


class 
foo implements displayable,printable 
  function 
display() { 
    
// code
  


  function 
doprint() { 
    
// code 
  


?>

Interface using simplifies the understanding of the code. We can see that class uses displayable and printable interfaces, it means that class should have such methods as display() and doprint().

Abstract classes

Abstract class is a class that can be used only as a base class. In abstract class you can define methods and variables. You can also define the abstract methods in the abstract class.

<?php 
abstract class foo 
  protected 
$x

  abstract function 
display(); 

  function 
setX($x) { 
    
$this->$x
  } 



class 
foo2 extends foo 
  function 
display() { 
    
// code
  


?> 

__call

In PHP 5 you can use the special method _call(). That method is used for searching of “unrealized” methods in that class.

<?php 
class foo 

  function 
__call($name,$arguments) { 
    print(
"Called? -  $name!"); 
  } 


$x = new foo(); 
$x->doStuff(); 
$x->fancy_stuff(); 
?>  

That special method can be used for methods overloading. You can analyze received methods and depending on the result call the required closed method:

<?php 
class Magic 

  function 
__call($name,$arguments) { 
    if(
$name=="foo") { 
      if(
is_int($arguments[0])) $this->foo_for_int($arguments[0]); 
      if(
is_string($arguments[0])) $this->foo_for_string($arguments[0]); 
    } 
  } 

  private function 
foo_for_int($x) { 
    print(
"Look, the whole number!"); 
  } 

  private function 
foo_for_string($x) { 
    print(
"look, a line!"); 
  } 


$x = new Magic(); 
$x->foo(3); 
$x->foo("3"); 
?>  

__set and__get

Now you can use methods __set and __get for searching all the attempts of changing or accessing the undefined variables.

<?php 
class foo 

  function 
__set($name,$val) { 
    print(
"Hello, you tried to set value $val to $name variable"); 
  } 

  function 
__get($name) { 
    print(
"Hello, you tried to call $name"); 
  } 


$x = new foo(); 
$x->bar 3
print(
$x->winky_winky); 
?> 

Type specifying for arguments

In PHP 5 you can “tell” the method that it should get the definite type object as an argument:

<?php 
class foo 
  
// code ... 


class 
bar 
  public function 
process_a_foo(foo $foo) { 
   
// any code 
  



$b = new bar(); 
$f = new foo(); 
$b->process_a_foo($f); 
?>  

Static class terms

Static terms and static methods can be used for realizing “class methods” and “class variables”.

“Class static method” is a method that can be called without creating the object of that class. “Class variable” is a variable that can be called without creating the object of that class (access method isn’t required).

<?php 
class calculator 
  static public 
$pi 3.14151692

  static public function 
add($x,$y) { 
    return 
$x $y
  } 


$s calculator::$pi
$result calculator::add(3,7); 
print(
"$result"); 
?> 

Exceptions

Exceptions are the standard method of errors processing in such languages as Java and C++; in PHP 5 exception intercept is carried out with the help of "try" - "catch".

<?php 
class foo 

  function 
divide($x,$y) { 
    if(
$y==0) throw new Exception("zero divide is impermissible"); 
    return 
$x/$y
  } 


$x = new foo(); 

try { 
  
$x->divide(3,0);    
} catch (
Exception $e) { 
    echo 
$e->getMessage(); 
    echo 
"\n<br />\n";

?>  

Exceptions defined by user

You can define you own exceptions for problems processing in your program. The only thing you have to do is to extend Exception class having specified the class constructor and getMessage method.

<?php 
class WeirdProblem extends Exception 

   private 
$data

   function 
WeirdProblem($data) { 
        
parent::exception(); 
        
$this->data $data
    } 

    function 
getMessage() { 
        return 
$this->data " Called strange exception"
    } 

?>

Then use throw new WeirdProblem($foo) construction

Namespaces

For more comfort classes and functions can be grouped to the namespaces.

<?php 
namespace Math 


  class 
Complex 
    
//...???... 
    
function __construct() { 
      print(
"Hello"); 
    } 
  } 


$m = new Math::Complex(); 
?>

Pay attention on the syntax of namespaces using for class indicating.


 
  • Top