TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-27-2009, 11:13 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default 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) { } 
??
__________________
Tanax is offline  
Reply With Quote
Old 11-27-2009, 11:31 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,296
Thanks: 17
Village Idiot is on a distinguished road
Default

No. PHP does not support overloading.
__________________

Village Idiot is offline  
Reply With Quote
Old 11-27-2009, 11:59 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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

Why?
__________________
Tanax is offline  
Reply With Quote
Old 11-28-2009, 01:05 AM   #4 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

Lazyness. In general PHP's OOP-support is quite outdated.
Izym is offline  
Reply With Quote
Old 11-28-2009, 01:22 AM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,296
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Old 11-28-2009, 03:19 AM   #6 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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'); 
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 11-28-2009, 03:29 AM   #7 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,296
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Old 11-28-2009, 03:58 AM   #8 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

Very true, but when used correctly func_get_args() can be an extremely powerful asset.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 11-28-2009, 04:51 AM   #9 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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'); 
__________________
My Site
adamdecaf is offline  
Reply With Quote
Old 11-28-2009, 01:50 PM   #10 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
Very true, but when used correctly func_get_args() can be an extremely powerful asset.
With great power comes great responsibility ;)
Izym is offline  
Reply With Quote
Old 11-30-2009, 03:20 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by ETbyrne View Post
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 View Post
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.
__________________
Tanax is offline  
Reply With Quote
Old 11-30-2009, 08:48 PM   #12 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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': ...
delayedinsanity is offline  
Reply With Quote
Old 11-30-2009, 10:22 PM   #13 (permalink)
The Addict
 
adamdecaf's Avatar
 
Join Date: May 2009
Posts: 287
Thanks: 5
adamdecaf is on a distinguished road
Default

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
)); 
__________________
My Site
adamdecaf is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 07:55 AM
Can't get PHP 5. to work? Newbie windows PHP guy DotNetTim Absolute Beginners 6 02-07-2009 10:25 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 01:14 AM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM


All times are GMT. The time now is 05:06 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design