seventheyejosh Posted September 16, 2009 Share Posted September 16, 2009 Is there any way to do something like this? class a{ protected $name='Josh'; } class b extends a{ function show_name($name=$this->name){ echo $name; } } so that if there is no specified value, it'll default to the parent's definition. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/ Share on other sites More sharing options...
Highlander Posted September 16, 2009 Share Posted September 16, 2009 class b extends a{ function show_name($name = null){ if ($name != null) echo $name; } else { echo $this->name; } } } That should work Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919726 Share on other sites More sharing options...
seventheyejosh Posted September 16, 2009 Author Share Posted September 16, 2009 Is there not a more elegant way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919732 Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 class b extends a{ function show_name($name = null) { echo (is_null($name)) ? $this->name : $name; } } Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919738 Share on other sites More sharing options...
seventheyejosh Posted September 16, 2009 Author Share Posted September 16, 2009 lol I guess that techincally is more elegant, but I meant via the functions parameters. Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919746 Share on other sites More sharing options...
gevans Posted September 16, 2009 Share Posted September 16, 2009 What do you want to be more elegant about it?? Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919749 Share on other sites More sharing options...
Daniel0 Posted September 16, 2009 Share Posted September 16, 2009 You can only specify a constant value as default for an argument. Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919751 Share on other sites More sharing options...
seventheyejosh Posted September 16, 2009 Author Share Posted September 16, 2009 Yea I was just getting ready to say that I fixed it by setting it to a class constant: class config{ const server='localhost'; //server address const user='root'; //server username const pass='root'; //server password const db='small'; //database name //stuff } class sql extends config{ public function connect($server=parent::server,$user=parent::user,$pass=parent::pass,$db=parent::db){ $this->disconnect(); mysql_connect($server,$user,$pass); //connect to the server mysql_select_db($db); //select the database }//end function connect //stuff } Quote Link to comment https://forums.phpfreaks.com/topic/174493-solved-parent-variable-as-default-parameter/#findComment-919758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.