apelare proprietate protected din clasa

Discutii despre script-uri si coduri PHP-MySQL, precum si lucru cu XML in PHP.
andras
Mesaje: 430

apelare proprietate protected din clasa

Salut,
Am intr-o clasa o proprietate PROTECTED. Stiu ca se poate folosi numai in clasa si in sublase, dar cum o folosesc intr-o functie oarecare unde am instantiat clasa? Care este calea corecta de apelare? Multumesc.

MarPlo Mesaje: 4343
Salut
O solutie buna de a folosi datele dintr-o proporietate "protected" in afara clasei e sa fie returnata de o metoda /functie "public". In acea functie poti sa adaugi si verificare daca e permis sau nu apelarea proprietatii
Exemplu:

Cod: Selectaţi tot

class test {
  protected $a ='protected value';

  public function getA($p){
    if($p >8) return $this->a;
    else return 'Not for you';
  }
}

$obT = new test;
echo $obT->getA(7);  //Not for you
echo '<br>'. $obT->getA(9); //protected value 

andras Mesaje: 430
E perfect, dar mai am o intrebare: in constructorul clasei am variabile GLOBAL pe care le-am preluat dintr-un alt fisier. Le pot proteja si pe acestea? Adica sa le includ in clasa dar sa nu mai folosesc GLOBAL. Sau cum e mai bine sa procedez?

MarPlo Mesaje: 4343
Daca vrei sa folosesti mai multe proprietati "protected" in afara clasei, poti crea o singura metoda pt. asta.
Iar cu cele GLOBAL, pot fi retinute intr-un protected array in clasa si apelate tot cu o functie.
Poate iti e de folos acest exemplu:

Cod: Selectaţi tot

class test {
  protected $a ='protected value A';
  protected $b ='protected value B';
  protected $globals =[];  //store variable from GLOBALS

  function __construct(){
    //set GLOBALS variable in $globals
    $this->globals['vg1'] = $GLOBALS['vg1'];
    $this->globals['vg2'] = $GLOBALS['vg2'];
  }

  //return protected property with the name in $p (string with name), if allowed $rank
  public function getProt($p, $rank){
    $re ='Not for you';
    if(isset($this->{$p})){
      if($rank >8) $re = $this->{$p};
    }
    else $re ='Not property: '. $p;
    return $re;
  }

  //return item from protected $globals with the key $g, if allowed $rank
  public function getGlobal($g, $rank){
    $re ='Not for you';
    if(isset($this->globals[$g])){
      if($rank >8) $re = $this->globals[$g];
    }
    else $re ='Not item: '. $g;
    return $re;
  }
}

//some variables
$vg1 ='v1 global';
$vg2 ='v_2 global';

//use data from test class
$obT = new test;
echo $obT->getProt('a', 7);  //Not for you
echo '<br>'. $obT->getProt('x', 9);  //Not property: x
echo '<br>'. $obT->getProt('b', 10); //protected value B

//using values from $globals
echo '<br>'. $obT->getGlobal('vg1', 4);  //Not for you
echo '<br>'. $obT->getGlobal('vgx', 9);  //Not item: vgx
echo '<br>'. $obT->getGlobal('vg2', 10);  //v_2 global 

andras Mesaje: 430
Mai am o nelamurire: intr-o clasa am pus (simplificat):

Cod: Selectaţi tot

<?php
protected $v = ['a'=>'anul','b'=>'luna','c'=>'oreluc']; 
 function __construct(){   
    GLOBAL $o_anul,$o_luna,$o_oreluc;
    $this->v = [$o_anul=>$this->v['a'],$o_luna=>$this->v['b'],$o_oreluc=>$this->v['c']];
  }
?>
Mai este nevoie sa fac un alt array PROTECTED cu variabilele globale? Acestea sint preluate din alt fisier (din afara clasei), si nu se folosesc decit pentru popularea lui protected $v. Poate ramine in forma asta?

MarPlo Mesaje: 4343
Daca nu mai ai nevoie de proprietatea $v in forma initiala, e bine cum ai facut.