<?php
class Pet
{
public $name;
public function __construct ( $name )
{
$this->name = $name;
}
public function speak ($what)
{
return $what;
}
}
class Dog extends Pet
{
// Le he metido el null para que el parametro sea opcional
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 . PHP_EOL;
echo 'Speak of the super class: ' . $charlie->speak('hello, my name is ' . $charlie->name) . PHP_EOL;
echo 'Speak of the dog class: ' . $charlie->speak() . PHP_EOL;
Strict Standards: Declaration of Dog::speak() should be compatible with that of Pet::speak() in /in/T8pXB on line 41
The dog's name is Charlie
Speak of the super class: hello, my name is Charlie
Speak of the dog class: Woof Woof!
Output for 5.0.0 - 5.0.1
Strict Standards: Declaration of Dog::speak() should be compatible with that of Pet::speak() in /in/T8pXB on line 41
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/T8pXB on line 45
The dog's name is CharliePHP_EOL
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/T8pXB on line 46
Speak of the super class: hello, my name is CharliePHP_EOL
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/T8pXB on line 47
Speak of the dog class: Woof Woof!PHP_EOL
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/T8pXB on line 5
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/T8pXB on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/T8pXB on line 5
Process exited with code 255.