<?php
class Pet
{
public $name;
public function __construct ( $name )
{
$this->name = $name;
}
public function speak ($what)
{
return $what;
}
}
class Dog extends Pet
{
public function speak ( $what = null )
{
return $what ? parent::speak($what) : 'Woof Woof!';
/*
// Otra alternativa para meter codigo mas complicado seria:
if($what)
return parent::speak($what);
return 'Woof Woof';
*/
}
public function plays()
{
return 'fetch';
}
}
$charlie = new Dog ( 'Charlie' );
echo 'The dog\'s name is ' . $charlie->name . '<br/>';
echo 'Speak of the super class: ' . $charlie->speak('hello, my name is ' . $charlie->name) . '<br/>';
echo 'Speak of the dog class: ' . $charlie->speak() . '<br/>';
The dog's name is Charlie<br/>Speak of the super class: hello, my name is Charlie<br/>Speak of the dog class: Woof Woof!<br/>
Output for 4.4.2 - 4.4.9
<br />
<b>Parse error</b>: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/mO5X8</b> on line <b>5</b><br />
Process exited with code 255.
<br />
<b>Parse error</b>: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in <b>/in/mO5X8</b> on line <b>5</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br />
<b>Parse error</b>: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in <b>/in/mO5X8</b> on line <b>5</b><br />
Process exited with code 255.