Mat's Blog

Random Thumbnail
Random Image

Overloading PHP functions and methods

In a nutshell, it's not possible.

PHP only allows one function or method with a name to exist per class and only one constructor per class. Originally, I was going to write a class called "Entry" which had two constructors. One constructor would receive arguments for all of the attributes of the object and a second constructor would receive an id number as an argument and retrieve the attributes from the database. Now, I'll have to create an empty object and just call a method that does what I want e.g.: Retrieving the data from the database and filling in all of the attributes I give it.

Now, a "magic" method exists represented by "__call" with two passed value: $name and "arguments. The first argument, $name, is the name of the method that you just called. $arguments is an array of all the values you passed to the method.

So if I have an object called "$car" and I called a function like so: $car->conversion($value1, $value2), the $name variable has the name of the method I called ("conversion") and an array containing $value1 and $value2.

Posted on January 13, 2010 at 11:57 am. Tags: php admin


Leave a Comment:

Name:

E-Mail Address:

URL:

Comment:

Copyright 2009-2010 Kakomu + Mat T