December
11
2008

protected nelle classi in Php - Corso Php

Tutorial->Programmazione-tutorial->Tutorial Php

  1. <?php
  2. class Linka{
  3.     public $interna = 12;
  4.     static $prova;
  5.     protected  $protetta = "13";
  6.    
  7.     function __construct(&$passo){
  8.        $passo=13;
  9.        $this->protetta = 12;
  10.        
  11.        
  12.     }
  13.     function __clone()
  14.     {
  15.         echo "scatto" ;
  16.         self::$prova=12;
  17.         $this->interna =13;
  18.        
  19.     }
  20.    
  21. }
  22. class figlia extends Linka
  23. {
  24.   function __construct(){
  25.        
  26.      
  27.       echo  "protetta:" . $this->protetta . "fine proge";
  28.     }
  29.    
  30. }
  31.  
  32.  
  33. function testa($valore1,$valore2 )
  34. {
  35.     $args = (func_get_args());
  36.     foreach($args as $value)
  37.     echo $value;
  38.    
  39. }
  40.  
  41.  
  42. $p = new figlia();
  43. //exit;
  44.  
  45.  
  46. testa(12,13,14);
  47.  
  48. $vardiprova = 12;
  49. try{
  50.    
  51.    
  52.     //throw new Exception("Non posso procedere!");
  53.     $test = new Linka($vardiprova );
  54.     echo Linka::$prova;
  55.    
  56.     $newtest = clone $test  ;
  57.     echo($newtest->interna);
  58.     echo Linka::$prova;
  59.     echo $test->interna ;
  60. }
  61. catch(Exception $e) {
  62. echo $e;
  63.    
  64. }
  65.  
  66. ?>
  67.  

Usiamo l'attributo protected qualora vogliamo che solo le classi figlie abbiano visibilità sui quei metodi ed attributi.

Usando static non è necessario istanziare la classe per poter utilizzare quel metodo o attributo dichiarato come static.

Per accedere ad una var protetta da una classe figlia : self::$prova .

Per accedere da una figlia ad una padre : $this->protetta, stando attenti a non porre il dollaro.





Pagina 0 di 1
Ordina per Nome Data