Function Definition help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nova
Forum Newbie
Posts: 2
Joined: Sun Jun 02, 2002 1:01 am

Function Definition help

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
nova
Forum Newbie
Posts: 2
Joined: Sun Jun 02, 2002 1:01 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

:D
Post Reply