Get up to 80 % extra points for free! More info:

Discussion – Lesson 7 - Inheritance in PHP

Back

 

Comments
Avatar
Justin Jake Telo:2/9/2018 22:33

<?php

// Contents of the Human.php file
class Human
{

public $firstName;
public $lastName;
public $age;
private $tiredness = 0;

public function __construct($fir­stName, $lastName, $age)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->age = $age;
}

public function sleep($time)
{
$this->tiredness -= $time * 10;
if ($this->tiredness < 0)
$this->tiredness = 0;
}

public function run($distance)
{
if ($this->tiredness + $distance <= 20)
$this->tiredness += $distance;
else
echo("I'm too tired.");
}

public function greet()
{
echo('Hi, my name is ' . $this->firstName);
}

public function __toString()
{
return $this->firstName;
}

}

// Contents of the PhpProgrammer.php file
class PhpProgrammer extends Human
{

public function program()
{
echo("I'm programming...");
}

}
//Contents of the webDeveloper.php file
class webDeveloper extends Human
{
public function webdev()
{
echo("hi im " . $this->firstName . " I'm a web developer...");
}
}

// Contents of the index.php file

require_once('clas­ses/Human.php');
require_once('clas­ses/PhpProgram­mer.php');

$carl = new Human('Carl', 'Smith', 30);
$john = new PhpProgrammer('Joh­n', 'Newman', 24);
$tin = new webDeveloper('Tin','T­elo',28);

$carl->run(10);
$carl->run(10);
$carl->run(10);
$john->greet();
echo('<br />');
$john->program();
echo('<br />');
$tin->webdev();
$tin->run(400);
// $carl->webdev(); cant' call its decedant 'WebDeveloper Class';
/* $john->webdev(); this is an error this parent the parents of webDeveloper
'$jonh-> = new PhpProgrammer'*/

 
Reply
2/9/2018 22:33
Avatar
Justin Jake Telo:2/9/2018 22:39

im talking the parents and child ('ancestor and descendant')
decendant can use the method of their ancestor but the ancestor can't use the methods of its decendants.. but why there is code that extends it for there relationship please enlighten me

 
Reply
2/9/2018 22:39
Avatar
Justin Jake Telo:2/10/2018 0:36

can you help me on this.. i got to call the protected method in via $carl = new Human and protected function tin() inside the class of human i once call it but i got this result

PHP Fatal error: Call to protected method Human::tin() from context '' in /home/jailphp/5a7e8309­70c31/index.php on line 77

here's ,my code

<?php

// Contents of the Human.php file
class Human
{

public $firstName;
public $lastName;
public $age;
private $tiredness = 0;

public function __construct($fir­stName, $lastName, $age)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->age = $age;
}

protected function sleep($time)
{
$this->tiredness -= $time * 10;
if ($this->tiredness < 0)
$this->tiredness = 0;
}
protected function tin(){
echo ($this->firstName . " " . $this->lastName);
}

public function run($distance)
{
if ($this->tiredness + $distance <= 20)
$this->tiredness += $distance;
else
echo("I'm too tired.");
}

public function greet()
{
echo('Hi, my name is ' . $this->firstName);
}

public function __toString()
{
return $this->firstName;
}

}

// Contents of the PhpProgrammer.php file
class PhpProgrammer extends Human
{

public function program()
{
echo("I'm programming in {$this->ide}...");
}
protected function zion(){
return ($this->firstName . " " . $this->lastName);
}

}

// Contents of the index.php file
require_once('clas­ses/Human.php');
require_once('clas­ses/PhpProgram­mer.php');

$carl = new Human('Carl', 'Smith', 30);
$john = new PhpProgrammer('Joh­n', 'New', 24, 'Eclipse');

//$john->greet(); it work
//$carl->program(); it doesn't work
//$john->tin(); not working called in PhpProgrammer instanceprotected!!
//$carl->tin(); not working called in Human Class protected
//$carl->tin(); not working called in Human Class protected

?>

 
Reply
2/10/2018 0:36
Avatar
Replies to Justin Jake Telo
David Capka Hartinger:2/10/2018 4:56

Hi Justin,
you can call protected methods only from within the class. Only public methods can be called on the variable carrying the instance. You can call $this->tin() anywhere in the code of the PhpProgrammer class, but not outside of it.

Edited 2/10/2018 4:56
Reply
2/10/2018 4:56
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
Justin Jake Telo:2/10/2018 7:59

Thans david i fiugre it our.. like

class human {

protected function tin(){
echo ($this->firstName . " " . $this->lastName);
}
class PhpProgrammer extends human{

public function zion() {
echo $this->tin();
}
}
include(classes­s/Human.php);

$carl = new human();
$joseph = new PhpProgrammer();

$joseph->zion(); // in this i can call protected methods in parent human

 
Reply
2/10/2018 7:59
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

5 messages from 5 displayed.