maxtarco Posted June 5, 2009 Share Posted June 5, 2009 I tried fixing it, but don't know enough about functions. <?php class parents { var $name = ''; var $eyecolour = 'blue'; function setcolour($colour) { $this->eyecolour = $colour; } function getname() { if (empty ($this->name)) { return "This person does not have a name yet!"; } else { return parents::name; } } ************ I got stuck here ************ function cry(); } class child inherit parents { public private var $eycolor = 'brown'; function cry() { echo "cry"; } function parentname() { echo "My parents name is".$this->getname()." and their eye colour is ".$eyecolour; } } $parents = new parents("John"); $child = new child(); $child->parentname(); ?> ********************************************************************************** Looking at the data within the script, I'm expecting the output to be: My parents name is John and their eye colour is brown Quote Link to comment https://forums.phpfreaks.com/topic/161039-php-functions/ Share on other sites More sharing options...
Mark Baker Posted June 5, 2009 Share Posted June 5, 2009 Classes inherit from other classes, instances inherit from classes but not from other instances of the class // Instantiate a new instance ($parents) of class parents and assign a name value of 'John' to that instance of the class $parents = new parents("John"); // Instantiate a new instance ($child) of class child. // This inherits attributes and methods from the class parents, but not from any specific instance of the parents class (e.g. $parents) // Display the name attribute for the instance ($child) of the child class, which has inherited the name attribute from the parents class definition (where the default is an empty string). $child->parentname(); There is no relationship between the instances $child and $parents Quote Link to comment https://forums.phpfreaks.com/topic/161039-php-functions/#findComment-849910 Share on other sites More sharing options...
JonnoTheDev Posted June 5, 2009 Share Posted June 5, 2009 This is bad syntax public private var $eycolor = 'brown'; The var syntax is depriciated as of php5 and you can only use 1 type of access specifier i.e public $eyecolor; class parents { protected $name; protected $eyecolour; public function setcolour($colour) { $this->eyecolour = $colour; } } Quote Link to comment https://forums.phpfreaks.com/topic/161039-php-functions/#findComment-849922 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.