PHPBuilder - Introduction to PHP5 Page 6



RSS Twitter
Articles Tricks And Hacks

Introduction to PHP5 - Page 6

by: Luis Argerich
|
April 11, 2003

Type hinting

In PHP5 you will be able to indicate that a method must receive an object of some class as an argument.
Example 10: type hinting

<?php
class foo {
  
// code ...
}

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

$b = new bar();
$f = new foo();
$b->process_a_foo($f);
?>
As you can see the class name can be indicated before the argument name to make PHP5 know that $foo should be an object of the class foo.

Static members

Static members and static methods can be used to implement terms known in OOP as "class methods" and "class variables".
A "class method" is a method that can be called without creating an instance of the class.
A "class variable" is a variable that can be accessed without creating an instance of the class (and without needing a get method)
Example 11: class methods and class variables

<?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");
?>

« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Luis Argerich

Comment:



Comment:

(Maximum characters: 1200). You have characters left.