Home > PHP > PHP5 Tutorial – Not Pure Object Oriented Programming Approach

PHP5 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

Popular Articles:

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • email
  • IndianPad
  • LinkedIn
  • Live
  • MySpace
  • Netvibes
  • RSS
  • Technorati
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Reddit
  • Add to favorites
  • PDF
  • Twitter
Categories: PHP Tags: ,
  1. December 5th, 2007 at 00:29 | #1

    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/

  2. December 7th, 2007 at 02:41 | #2

    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.

  3. StephenB
    January 17th, 2008 at 17:19 | #3

    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

  4. techdesk100
    March 18th, 2008 at 07:37 | #4

    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.

  5. May 3rd, 2009 at 22:23 | #5

    Missing function overloading is a major drawback, showing that PHP as OOP language is still very far behind.

    Example:
    You have a library of classes with class XXX, but one function FN in such a class needs to be changed.

    With FO:
    Inherit a new class from XXX, and overload FN. Neat!

    Without FO:
    Copy XXX to somewhere else, call it XXX1. Redo references to the file. Add FN to XXX.
    When XXX changes maintaining your system like this:
    1. Save FN from XXX1
    2. Copy the new XXX over XXX1
    3. Reinstate your function
    Clumsy like h…!

  6. Shailesh kumar
    July 15th, 2009 at 04:24 | #6

    Hi all ,

    if we put in conditions in the function for checking whether they are passed as a argument or not for method overloading, then what is a basic use of method overloading which allows a programmer to write two functions with the same name..

    so i think the example given above is correct ..

    but if we want to use it like

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

    then it is ok ..

    may be in next replease of PHP this problem will be solved :)

    Thanks

  7. September 2nd, 2009 at 06:47 | #7

    Hi

    I think function overloading is hardly used in real life programming. Why would you want to use the name of an existing function when you can use a different one.

    Can anyone elaborate?

    • September 2nd, 2009 at 07:24 | #8

      Function Overloading is one of the key component of Object Oriented Programming. It is widely used in .Net, Java and C++

  8. Sharad
    January 30th, 2010 at 04:53 | #9

    Hello Hitesh,

    I think there is auto data typing is the problem in PHP which make overloading typical. If we use two functions with same name with different number of Parameter then the same work we can do as follow-
    public function setName($abc, $xyz = false)
    {
    }
    Now we can pass $xyz or we can leave and we can write different code for both cases by checking if $xyz is passed or not.
    But if we want to use two function with same number of parameters of different data types, then it is the problem with PHP. so Its not allowed function overloading but partially support by above method.

    m i right?

    Sharad

  1. No trackbacks yet.