TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Does PHP allow this? (http://www.talkphp.com/general/5139-does-php-allow.html)

Tanax 11-27-2009 11:13 PM

Does PHP allow this?
 
Does PHP allow multiple methods inside a class with the same name but with different arguments?

PHP Code:

public function myFunction($argument) { }

public function 
myFunction($argument$argument2) { } 

??

Village Idiot 11-27-2009 11:31 PM

No. PHP does not support overloading.

Tanax 11-27-2009 11:59 PM

Ah, it's called overloading.
Alright. Too bad.

Why? :-P

Izym 11-28-2009 01:05 AM

Lazyness. In general PHP's OOP-support is quite outdated.

Village Idiot 11-28-2009 01:22 AM

Overloading can be applied in object oriented programming, but does not inherently belong to it. I've been out of the PHP game for too long to comment on what OOP abilities it has, especially because I tend to confuse what it can do to what C++ can do.

ETbyrne 11-28-2009 03:19 AM

PHP Code:

class myClass
{
    public function 
myFunction()
    {
        
$arguments func_get_args();
    }


You can then use any number of arguments on that function.

PHP Code:

$obj = new myClass();
$obj->myFunction();
$obj->myFunction(1,'two');
$obj->myFunction(3,4,5,'six'); 


Village Idiot 11-28-2009 03:29 AM

edit: That could get real messy real fast depending on the function. Be very careful if you are going to use that as a substitute to overloading.

ETbyrne 11-28-2009 03:58 AM

Very true, but when used correctly func_get_args() can be an extremely powerful asset.

adamdecaf 11-28-2009 04:51 AM

You can always do this.

PHP Code:

function my_funct($parm1 ''$parm2 '') {



Where the default value is assigned to the argument and overwritten by the function call.

PHP Code:

function my_funct($first 'John'$last 'Doe') {
   echo 
$first ' ' $last;
}

// Just a note, the optional arguments are best
// added last in the order, otherwise you will 
// start to get syntax/logic errors.

// Adam Doe
my_funct('Adam'); 


Izym 11-28-2009 01:50 PM

Quote:

Originally Posted by ETbyrne (Post 29359)
Very true, but when used correctly func_get_args() can be an extremely powerful asset.

With great power comes great responsibility ;)

Tanax 11-30-2009 03:20 PM

Quote:

Originally Posted by ETbyrne (Post 29357)
PHP Code:

class myClass
{
    public function 
myFunction()
    {
        
$arguments func_get_args();
    }


You can then use any number of arguments on that function.

PHP Code:

$obj = new myClass();
$obj->myFunction();
$obj->myFunction(1,'two');
$obj->myFunction(3,4,5,'six'); 


Quote:

Originally Posted by adamdecaf (Post 29360)
You can always do this.

PHP Code:

function my_funct($parm1 ''$parm2 '') {



Where the default value is assigned to the argument and overwritten by the function call.

PHP Code:

function my_funct($first 'John'$last 'Doe') {
   echo 
$first ' ' $last;
}

// Just a note, the optional arguments are best
// added last in the order, otherwise you will 
// start to get syntax/logic errors.

// Adam Doe
my_funct('Adam'); 


True, the calling of the method would become like I want it.
Although the second example would not be exactely like I want it since I still would have to define how many arguments it would need.

However, the calling of the method is not the only thing. It's how things are handled within the method. If using your example:
PHP Code:

$obj->myMethod(1);
$obj->myMethod(1"String"); 

This would still be in the same method and the method would become quite large depending on what the method does.

delayedinsanity 11-30-2009 08:48 PM

Assuming it wouldn't be easier to just rename the function for different uses (don't overload if you don't have to), if you were determined to use the same name you could always use a switch.

php Code:
$num  = func_num_args();
$args = func_get_args();

switch ( $num ) {
    case 1:
        some_func();
        break;

    case 2:
        some_other_func();
        break;
}

You could alternatively use count() on $args, or place your code where the some_funcs() are... same theory applies to overloading for the purposes of dealing with different objects,

php Code:
function fake_overload( $obj ) {

    if ( ! is_object( $obj ) )
        return;

    $reflect = new ReflectionClass( $obj );

    switch ( $reflect->getName() ) {
        case 'first_object': ...

adamdecaf 11-30-2009 10:22 PM

You could also try something similar to this, it's not overloading, but still allows multiple amounts of arguments. This would really allow you to send any number of parameters and values, it would just take a while to process if you have a lot.

PHP Code:

function d($data = array()) {
   foreach(
$data as $key => $value) {
       switch (
$key) {
           
'speak'
              echo 
$value;
           break;
          
           
'save':
              
$this->db->save($value);
           break;
       }
   }
}

d(array(
   
'speak' => 'Adam'
));

d(array(
   
'speak' => 'John',
   
'save' => $name
)); 



All times are GMT. The time now is 02:53 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0