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

&lt;html&gt;&lt;body&gt;&lt;?php class cbase
{
	function cbase() { print('base constructed&lt;br&gt;'); }
	function doSomething() { print('base function&lt;br&gt;'); }
}

class cderived extends cbase
{
	function cderived() { print('derived constructed&lt;br&gt;'); }
	function doSomething() { print('derived function&lt;br&gt;'); }
}
$cd = new cderived;
$cd-&gt;doSomething();
?&gt;&lt;/body&gt;&lt;/html&gt;
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!!!! :oops:

thanks for the help

Posted: Sun Jun 02, 2002 2:57 am
by volka
:D