wolves Posted April 7, 2006 Share Posted April 7, 2006 is it possible? if is, how to do?like this[code]function bar() { return "hello";}class foo { public function __construct($str = bar()) { echo $str; } }[/code]it's not work, but have other syntax to do it? Link to comment https://forums.phpfreaks.com/topic/6811-object-question/ Share on other sites More sharing options...
wildteen88 Posted April 7, 2006 Share Posted April 7, 2006 You can do this:[code]__construct($str = "hello")[/code]You cannot call a function when when using parameters. Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24778 Share on other sites More sharing options...
Zane Posted April 7, 2006 Share Posted April 7, 2006 I would think that would work at first glance, but class integration with a regular PHP file is kinda hectic anywayI would say if it doesn't work, you could *grits teeth* make it globalmost of the time it's not a good idea to make things global but if it works it worksand the best alternative I could think of would be to make the function bar just a static method in your classbut that just depends on how much bar() has to do with the class at allsee example[code]class foo { public function __construct($str = foo::bar()) { echo $str; } public static function bar() { return "hello"; }}[/code][!--quoteo(post=362550:date=Apr 7 2006, 11:29 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Apr 7 2006, 11:29 AM) [snapback]362550[/snapback][/div][div class=\'quotemain\'][!--quotec--]You cannot call a function when when using parameters.[/quote]are you sure....I've never ran into such a problem as what wolves is doingbut I would think that you could very well use a function to declare an argument valueit works for any other function[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]echo date("g:m", [!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]strtotime()[!--colorc--][/span][!--/colorc--])[/quote]but maybe classes are a different story I dunno...I'm not on my own machine Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24781 Share on other sites More sharing options...
wildteen88 Posted April 7, 2006 Share Posted April 7, 2006 No I meant when assigning parameters inside of functions ie:[code]function bar(){ return 'hello';}//you cant do this:function foo($str=bar()){ echo $str;}foo();//But you can do this.foo(bar());[/code]I know you can use functions when you are parsing parameters to functions , but not when you are assigning defualt values for parameters in side a function. Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24786 Share on other sites More sharing options...
Zane Posted April 7, 2006 Share Posted April 7, 2006 hmm....learn something everydaywell I guess you'd have to do this then wolves[code]class foo { public $string = foo::bar(); public function __construct($str = $this->string) { echo $str; } public static function bar() { return "hello"; }}[/code]notice it's still static Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24788 Share on other sites More sharing options...
wolves Posted April 7, 2006 Author Share Posted April 7, 2006 [!--quoteo(post=362560:date=Apr 7 2006, 10:52 AM:name=zanus)--][div class=\'quotetop\']QUOTE(zanus @ Apr 7 2006, 10:52 AM) [snapback]362560[/snapback][/div][div class=\'quotemain\'][!--quotec--]hmm....learn something everydaywell I guess you'd have to do this then wolves[code]class foo { public $string = foo::bar(); public function __construct($str = $this->string) { echo $str; } public static function bar() { return "hello"; }}[/code]notice it's still static[/quote]get unexpected '(', expecting ',' or ';', think it's not work Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24811 Share on other sites More sharing options...
wildteen88 Posted April 7, 2006 Share Posted April 7, 2006 You cant to do that either. You cannot call or use a function when delacaring parameters in a function:[code]funcion foo($var=bar())[/code] or:[code]$val = bar();function($var=$val)[/code]The only way to set a defualt value of a parameter in a function is to type it in manually ie:[code]fucntion foo($var="hello")[/code]This also applies to classes and methods too. Link to comment https://forums.phpfreaks.com/topic/6811-object-question/#findComment-24812 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.