Ma forse mi conviene vederli in blocco con l'ereditarietà.
Questo è il link di riferimento.
Eseguo pedissequamente o quasi...
Molto semplice e banale: questo è l'esempio che ho trovato sul tutorial
<?php
class person{
var $name;
public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name){
$this->name=$persons_name;
}
function set_name($new_name){
$this->name=$new_name;
}
function get_name(){
return $this->name;
}
}
$stefan=new person("Stefan Mischook");
echo "Stefan's full name ".$stefan->get_name();
echo "Tell me private stuff: ".$stefan->pinn_number;
?>
La variabile private non può venir letta da fuori della classe.Comportamento del tutto analogo a ciò che accade in altri linguaggi OOP.
Ora vediamo l'ereditarietà.
<?php
class person{
var $name;
public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name){
$this->name=$persons_name;
}
public function set_name($new_name){
$this->name=$new_name;
}
public function get_name(){
return $this->name;
}
}
class employee extends person{
function __construct($employees_name){
$this->name=$employees_name;
}
}
$stefan=new person("Stefan Mischook");
$james=new employee("Johnny Fingers");
echo "----->".$james->get_name();
?>
----->Johnny FingersBene. Abbiamo visto l'ereditarietà e i modificatori di accesso.
L'ultima cosa che devo vedere è l'overriding dei metodi (che comunque è uguale a quello che conosco da altri linguaggi OOP).
Nessun commento:
Posta un commento