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 01-24-2008, 09:19 PM   #1 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default 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.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 01-24-2008, 09:50 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-27-2008, 05:56 PM   #3 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

No that doesn't work.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-27-2008, 06:04 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-27-2008, 06:13 PM   #5 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

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.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-27-2008, 06:21 PM   #6 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-27-2008, 06:24 PM   #7 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

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?
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 02-27-2008, 06:32 PM   #8 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

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

PHP Code:
<?php

textDcoration
('myFont'12nulltrue);
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-27-2008, 06:32 PM   #9 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

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 :)
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko 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


All times are GMT. The time now is 11:22 AM.

 
     

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