Custom Search

PHP 5 Tutorial - Not Pure Object Oriented Programming Approach

PHP development team has come up with great improvement in PHP5, one such feature is the introduction of improved Object Oriented Concept in PHP5, But it lacks the pure Object Oriented Programming approach. In this article i will be discussing few bugs that exists in the OOPS implementation in PHP5 and the main culprit behind this bugs are:





Loosely typed behavior of PHP Language
This will the major problem that PHP programmer will face while migrating to PHP5 from PHP4. In Object Oriented Programming the data members that the class is going to use should be defined before defining the constructors and methods for the class. But due to the loosely typed behavior of PHP5 following bugs exists for loosely typed behaviour.

Automatically creates variables for class while assigning values to it
Now if you are accessing an undefined class variable, PHP5 does not throws an error instead it automatically creates that undefined variable.
Example:

< ?
class employee
{
 public function setName($name)
 {
    $this->name = $name;    //Automatically creates $name variable for employee class
 }
 public function getName()
 {
     return $this->name;
 }
}
$employee = new employee();
$employee->setName("Hitesh");
echo $employee->getName();
echo "<br />".$employee->name;
?>

Output:
Hitesh
Hitesh
Explanation: When the above code snippet is executed $name is automatically created inside setName() method.
Example:

< ?
class employee
{
    public function __construct()
    {
	echo "Constructor Called!!!";
    }
}
$employee = new employee("Hitesh");    //Should should throw an error as the constructor does not any argument defined
?>

Output:
Constructor Called!!!
Explanation: Here i am instantiating class object and passing 1 parameter “Hitesh”, but in the __construct function i have not defined any parameter but still PHP5 accepts it.

Function Overloading not Supported
One the main feature of Polymorphism in Object Oriented Language is Function Overloading but unfortunately PHP5 does not supports it.
Function Overloading is the technique wherein class can have more than one method with same name but the parameters differ by the parameter count, type and position.
Example - Function OverLoading Usage

< ?
public function setName($name)
{
 
}
 
public function setName($fname,$lname)
{
 
}
?>

Example - Using Function Overloading in PHP5

< ?
class employee
{
    private $name;
    private $fname;
    private $lname;
    public function setName($name)   
    {
	$this->name = $name;
    }
 
    public function setName($fname,$lname)    //Method setName redeclared again
    {
	$this->fname = $fname;
	$this->lname = $lname;
    }
}
$employee = new employee("Hitesh");
?>

Output:
Cannot redeclare employee::setName()
Explanation: In the above snippet i have declared two methods setName() but as PHP5 does not supports Function Overloading the PHP5 throws an error.

Custom Search

Related Post

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

I do not agree with you on this…

Both instances you mention I find OK to live with.

- In the first case I do not mind the outcome, it’s like a shortcut ;)

- In the second case, you can easily simulate function overloading…

public function setName($name, $lname = false)
{

}

you can now call the function with the $lname parameter or without, it’s your choice … and in the function body you can make cases on what to do if lname is sent and what if it isn’t …

Correct me if I’m wrong…

All best,

Petar
http://boss.peconi.com/

Hi Peter,
But still you can only declare one function with setName().

But Function Overloading allows you to declare multiple functions with the same name.

Hi
Agree in part.
First one. I’d call sloppy programming anyway.
Second. I’d like to have see E_STRICT call this. But the overloading (and catching through funct_get_args) appears premeditated. Not a bug as such, just a do not like.
Third. Not going to happen in php. Look at the dev notes for php6.

Stephen

As far as I know the function is like the old function header in C. Which means if you leave it empty you can have 0 or more parameter arguments, and you have to detect it.

Leave a comment

(required)

(required)