alias301 Posted February 13, 2009 Share Posted February 13, 2009 is there any way for me to simulate this? <?php class Example { public $test_var = "Test"; public function test( $message=$this->test_var ) { // <= this is were I get the error message echo $message; } } $example = new Example; $example->test(); ?> any help is appreciated! Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/ Share on other sites More sharing options...
samshel Posted February 13, 2009 Share Posted February 13, 2009 you cannot put dynamic variable as default parameter. but..u can achieve the same results by this <?php class Example { public $test_var = "Test"; public function test( $message="" ) { // <= this is were I get the error message if($message == "") $message = $this->test_var; echo $message; } } $example = new Example; $example->test(); ?> Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/#findComment-761745 Share on other sites More sharing options...
alias301 Posted February 14, 2009 Author Share Posted February 14, 2009 is there any other way that is better and cleaner??? thanks for the reply! Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/#findComment-761749 Share on other sites More sharing options...
samshel Posted February 14, 2009 Share Posted February 14, 2009 not that i know of Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/#findComment-761750 Share on other sites More sharing options...
Maq Posted February 14, 2009 Share Posted February 14, 2009 What's the point of giving test() a parameter when it doesn't need it. You're assigning that parameter the class variable anyway. Also, when you call the function you don't give it a parameter... Why not do something like this: class Example { public $test_var = "Test"; public function test() { // echo $this->test_var; } } $example = new Example; $example->test(); ?> Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/#findComment-761766 Share on other sites More sharing options...
samshel Posted February 14, 2009 Share Posted February 14, 2009 he wants to use the member variable as a parameter only if the parameter is not passed externally... Link to comment https://forums.phpfreaks.com/topic/145134-default-param-in-class-function/#findComment-761767 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.