| | |
Overloading PHP Functions
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I know it is possible to overload PHP functions as I have seen this done with many of the prebuilt functions. My question is how do you declare an overloaded function? Everytime I try doing this I always get some error about missing arguments (even if there exsist a function with the same number of arguments)
Found this quote:
and this quote:
on this page
•
•
•
•
PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions.
•
•
•
•
Method overloading is however permitted.
<?php
class A {
function A() { }
function ech() {
$a = func_get_args();
for( $t=0;$t<count($a); $t++ ) {
echo $a[$t];
}
}
}
$test = new A();
$test->ech(0,1,2,3,4,5,6,7,8,9);
?>
// output:
// 0123456789
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
Actually, method overloading refers to the ability to use the same method/function name in multiple classes such as:
//in use:
As you can see, the same method name was used in both classes and can be called in the same php file. Hope this helps
php Syntax (Toggle Plain Text)
class test1 { private $value; //constructor function test1(){} //method function setValue($value){ $this->value=$value; } } class test2 { private $value; //constructor function test2(){} //method function setValue($value){ $this->value=$value; } }
//in use:
php Syntax (Toggle Plain Text)
<?php $test1=new test1(); $test1->setValue(1); $test2=new test2(); $test2->setValue(2); ?>
As you can see, the same method name was used in both classes and can be called in the same php file. Hope this helps
Last edited by peter_budo; Jun 17th, 2009 at 4:19 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
•
•
I know it is possible to overload PHP functions as I have seen this done with many of the prebuilt functions. My question is how do you declare an overloaded function? Everytime I try doing this I always get some error about missing arguments (even if there exsist a function with the same number of arguments)
Read this http://www.onlamp.com/pub/a/php/2005...erloading.html
and
http://www.devshed.com/c/a/PHP/Overl...ng-with-PHP-5/
Last edited by adatapost; Jun 17th, 2009 at 12:45 am.
Failure is not fatal, but failure to change might be. - John Wooden
![]() |
Similar Threads
Other Threads in the PHP Forum
- Previous Thread: Search from multiple tables ??
- Next Thread: Simple PHP Cart
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner beneath binary broadband broken button cakephp checkbox class cms code compression countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html httppost image include insert integration ip javascript joomla limit link links login mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion remote script search searchbox server session sessions sms smtp source space sql strip_tags survey syntax system table tutorial update upload url validator variable video virus votedown web website window.onbeforeunload=closeme; youtube






