Reasoning for PHP5 Protected Variables

Can you explain what arguments would be related to why I would like to use "protected" and "public" or "private" for some class variables and methods in PHP5? I just found a case where it seemed to me that I needed to "protect", and either chose "public" or "private" based on the intention. Even when working in teams, I still have to find a case (based on my knowledge so far) about why “protected” are necessary for class variables and methods.

+5
source share
7 answers

For example, the Flourish library class fDate provides many functions, but not everything I need. So I expanded my class.

I soon learned that his internal variable, fDate :: $ date (time in seconds since 1970), was a private property. It made it impossibleaccess to it in my ThriveDate subclass. After the companion Flourish changed it to a property protected, I was able to use it and, therefore, adequately extend fDate.

Because you never know who might need to extend your class, and to what extent it is always better to protect each internal property in the future , unless there are good reasons for the property to never change subclasses .

TL; DR: , : . .

+5

protected , . , .

, public, , / . , " " , .

, private, .

+1

:

<?
// burguers.php

/* abstract */ class BurguerClass {
  // nobody knows about these secret function
  private function addSecretRecipeSauce() { ... }  

  // these one can can change in subclasses
  // cannot be called globally
  protected /* virtual */ function addBread() { ... }  
  protected /* virtual */ function addSalad() { ... }  
  protected /* virtual */ function addMeat() { ... }
  protected /* virtual */ function addExtraIngredients() { ... }

  // these one it can be access globally
  public /* virtual */ function makeBurguer() {
    $this->addBread();
    $this->addSalad();
    $this->addMeat();
    $this->addExtraIngredients();
    $this->addSecretRecipeSauce();
  }  
}

/* concrete */ class CheeseBurguerClass extends BurguerClass {
  protected /* virtual */ function addCheese() { ... }

  public /* override */ function makeBurguer() {
    $this->addBread();
    $this->addSalad();
    $this->addMeat();
    $this->addExtraIngredients();
    // we add this one:
    $this->addCheese();
  } 
}

/* concrete */ class RanchStyleBurguerClass extends BurguerClass {
  protected /* override */ function addExtraIngredients() { ... }
}

/* concrete */ class EastCoastVegetarianStyleBurguerClass extends BurguerClass {
  protected /* override */ function addMeat() {
    // use soy "meat"
  }
  protected /* override */ function addExtraIngredients() { ... }
}

function burguers_main() {
    $Cheesburguer = new CheeseBurguerClass();

    // can be access
    $Cheesburguer->makeBurguer();
}

// execute it
burguers_main();

?>

" Chesburguer" . C.E.O., . , "burguerClass" .

, "addBread", "addSalad", "addMeat", "addExtraIngredients", , , "makeBurguer".

"addSecretRecipe".

+1

, .

, , CA-Visual Objects, Clipper, VB ASP.

, , , .

, VB ASP Get, Let Set, .

, . , - ...

0

, . , .

0

OOP ( ). PHP - script , .

, OOP, Java, Bruce Ekcel - Thinking in Java ( Amazon , : http://www.mindviewinc.com/Books/downloads.html). , PHP. , PHP. PHP, Java ( , , Java , PHP).

, , . " API" " " (. "" ), " " " ".

, .

, : : /, , , . , , 2 , , , . setState() getState(), ($ this- > state = $param; $this- > ), , -write , , (, ).

There are several more or less deep similar topics in OOP, this getter / setter is probably the most important of the main ones.

So my tips are:

  • use OOP, this is your friend;
  • use PHP in the same way as JAVA (strict OOP language), and not as in FORTRAN.
0
source

All Articles