SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 34 | Next

Hasin Hayder

"Object-Oriented Programming with PHP5"

In PHP5, there are two ways you can write a constructor method inside a
class. The first one is to create a method with the name __construct() inside the
class. The second is to create a method naming exactly the same as class name. For
example if your class name is Emailer, the name of the constructor method will be
Emailer(). Let's take a look at the following class which calculates the factorial of
any number:
//class.factorial.php
class factorial
{
private $result = 1;// you can initialize directly outside
private $number;
function __construct($number)
{
$this->number = $number;
for($i=2; $i<=$number; $i++)
{
$this->result *= $i;
}
}
Chapter 2
[ 25 ]
public function showResult()
{
echo "Factorial of {$this->number} is {$this->result}. ";
}
}
?>
In the code above, we used __construct() as our constructor function. The
behaviour will be same if you rename the __construct() function as factorial().
Now, you may ask if a class can have constructors in both styles? This means a
function named __construct() and a function named the same as class name. So
which constructor will execute, or will they both execute? This is a good question.


Pages:
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6