TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Like in python you think you will be able to define argument.... (http://www.talkphp.com/general/2084-like-python-you-think-you-will-able-define-argument.html)

Nor 01-24-2008 09:19 PM

Like in python you think you will be able to define argument....
 
In python I forget what its called by you can assign arguments by name for example:

Code:

test(name=blah)
You think in the future PHP will have this option? I think it would be useful.

Alan @ CIT 01-24-2008 09:50 PM

If I understand you correctly, you can do this in PHP already (kinda)

For example:

PHP Code:

<?php

function sayHello($name)
{
    echo 
'Hello ' $name;
}

sayHello($name 'Alan');

// Outputs: Hello Alan

Although technically all this is doing is creating a new global variable called $name, then assigning it the value of 'Alan', then passing the $name variable to the sayHello() function.

Alan

Nor 02-27-2008 05:56 PM

No that doesn't work.

Alan @ CIT 02-27-2008 06:04 PM

What problem do you experiance when using the code? I've tested the code above on PHP 5.2.5 and it works fine.

Can you explain more what you are trying to achieve?

Alan

Nor 02-27-2008 06:13 PM

Have you used python before?

Code:

def example(opt,opt2 = 0):
    print opt2
example(opt2=10)

PHP you can't do that now that I know of.

Code:

function example($opt1,$opt2 = 'test')
{
    print $opt2;
}
example($opt2 = 'test2');//if this was python it would output 'test2' but no this is php and it'll output 'test' since its the first argument and you can't redefine argument parameters based on its argument name which sucks specially when you got options u want to initiate without initiating other options in a php function.


Alan @ CIT 02-27-2008 06:21 PM

ahh, I see what you mean now. The only way you could achieve that in PHP at the moment is to lump all your arguments into an array and parse the array to your function:

PHP Code:

<?php

function example($args)
{
 print 
$args['opt2'];
}

$args = array(
 
'opt1' => 'hello',
 
'opt2' => 'world'
);

example($args);
// Would print 'world'

Although you do loose the ability to provide default values to arguments when doing this.

As far as I am aware there are no plans to introduce this form of argument handling in PHP 5.3 or 6 but if you really want to see it, you should post the idea to the php-internals mailing list to see what they think.

Alan

Nor 02-27-2008 06:24 PM

Alright :)

But seriously don't you think this is a perfect idea? For functions like:

function textDecoration( $font, $size, $underlined = false, $strikethrough = false );

what if u wanted strikethrough and not underline?

Alan @ CIT 02-27-2008 06:32 PM

It would make like easier by not having to pad function calls:

PHP Code:

<?php

textDcoration
('myFont'12nulltrue);


abiko 02-27-2008 06:32 PM

It is a good solution in Python, but if you want this in PHP you're good to go with arrays like Alan said.
Also the same thing could be used in Javascript for defining some atribute (Umm - Prototype -
Code:

Effect.toggle( 'id', 'effect', {'duration':'1'} );
Like you said your function
Code:

function textDecoration( $font, $size, $underlined = false, $strikethrough = false );
PHP Code:

function textDecoration $fontOption$styleOption );
// -->
function textDecoration( array( 'font' => 'Arial'
                                
'size' => '12'
                               
), array( 'underlined' ) ); 

so you just check if size isn't specified - you set the default value for it.
If underlined specified - underline, if not - don't.

I mean you get the picture.
Hope I helped.
Thanx to Alan :)


All times are GMT. The time now is 10:54 AM.

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