Page 1 of 1
Function Definition help
Posted: Sun Jun 02, 2002 1:01 am
by nova
I was just wondering what a proper function definition looks like.
I have been currently using:
Code: Select all
function functionName($var1)
{
//code stuff
}
I understand that php is relativly datatype less (to some extent) but I was wondering if you needed to also define the type of the var that you are returning (ie. string, bool, int ...)
also I was wondering if php has function overloading capability.
I am using in in some of my code that
is working, but I thought I read somewhere that php didn't have that capability yet.
Thank ya'll
Posted: Sun Jun 02, 2002 2:16 am
by volka
there is no declaration of the return value of a function
it's always something like 'mixed'
manual chapter
you cannot overload global-scope functions
req.php:
Code: Select all
<?php function test() { ... }?>
index.php:
Code: Select all
<?php require('req.php');
function test() { ... }
test();
?>
will lead to
PHP Fatal error: Cannot redeclare test() (previously declared in <blablabla>:<line>) in <blablabla> on line <line>
even function test($param) {...} fails.
But you can overload methods of objects
there are some useful
user contributed notes on this topic in the manual
Code: Select all
<html><body><?php class cbase
{
function cbase() { print('base constructed<br>'); }
function doSomething() { print('base function<br>'); }
}
class cderived extends cbase
{
function cderived() { print('derived constructed<br>'); }
function doSomething() { print('derived function<br>'); }
}
$cd = new cderived;
$cd->doSomething();
?></body></html>
leads to
derived constructed
derived function
if you take out 'function cderived() {..}' the output is
base constructed
derived function
Posted: Sun Jun 02, 2002 2:51 am
by nova
after tinkering with my code that I thought I was overloading with I just realized that I had added an extra letter. Geese!!!!
thanks for the help
Posted: Sun Jun 02, 2002 2:57 am
by volka