Jump to content

PHP Functions


maxtarco

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/161039-php-functions/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/161039-php-functions/#findComment-849910
Share on other sites

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;
  }
}

Link to comment
https://forums.phpfreaks.com/topic/161039-php-functions/#findComment-849922
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.