PHP handleiding

PHP TUIS PHP Inleiding PHP installeer PHP sintaksis PHP opmerkings PHP veranderlikes PHP Echo / Druk PHP datatipes PHP Strings PHP-nommers PHP Wiskunde PHP konstante PHP-operateurs PHP As...Anders...Elseif PHP skakelaar PHP-lusse PHP funksies PHP-skikkings PHP Superglobals PHP RegEx

PHP- vorms

PHP-vormhantering PHP-vorm validering PHP-vorm word vereis PHP-vorm URL/e-pos PHP-vorm voltooi

PHP Gevorderd

PHP datum en tyd PHP sluit in PHP-lêerhantering PHP-lêer oop/lees PHP-lêer skep/skryf PHP-lêer oplaai PHP-koekies PHP-sessies PHP filters PHP-filters Gevorderd PHP-terugbelfunksies PHP JSON PHP-uitsonderings

PHP OOP

PHP Wat is OOP PHP Klasse/Objekte PHP Konstrukteur PHP vernietiger PHP Toegangswysigers PHP erfenis PHP konstante PHP Abstrakte Klasse PHP-koppelvlakke PHP eienskappe PHP statiese metodes PHP Statiese Eienskappe PHP naamruimtes PHP Iterables

MySQL- databasis

MySQL-databasis MySQL Connect MySQL Skep DB MySQL Skep tabel MySQL Voeg data in MySQL Kry Laaste ID MySQL Voeg veelvuldige in MySQL voorberei MySQL Kies Data MySQL Waar MySQL Bestel deur MySQL verwyder data MySQL-opdateringsdata MySQL-limietdata

PHP XML

PHP XML-ontleders PHP SimpleXML-ontleder PHP SimpleXML - Kry PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX-databasis AJAX XML AJAX Live Search AJAX-peiling

PHP voorbeelde

PHP voorbeelde PHP samesteller PHP vasvra PHP Oefeninge PHP-sertifikaat

PHP- verwysing

PHP Oorsig PHP Skikking PHP-kalender PHP datum PHP gids PHP fout PHP-uitsondering PHP lêerstelsel PHP filter PHP FTP PHP JSON PHP sleutelwoorde PHP Libxml PHP-pos PHP Wiskunde PHP Diverse PHP MySQLi PHP-netwerk PHP-uitsetbeheer PHP RegEx PHP SimpleXML PHP-stroom PHP-string PHP veranderlike hantering PHP XML-ontleder PHP zip PHP Tydsones

PHP OOP - Oorerwing


PHP - Wat is erfenis?

Oorerwing in OOP = Wanneer 'n klas van 'n ander klas afkomstig is.

Die kinderklas sal al die publieke en beskermde eiendomme en metodes van die ouerklas erf. Daarbenewens kan dit sy eie eienskappe en metodes hê.

'n Oorgeërfde klas word gedefinieer deur die extends sleutelwoord te gebruik.

Kom ons kyk na 'n voorbeeld:

Voorbeeld

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

// Strawberry is inherited from Fruit
class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}
$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>

Voorbeeld Verduidelik

Die Strawberry-klas word van die Fruit-klas geërf.

Dit beteken dat die Strawberry-klas die publieke $name en $color eienskappe sowel as die publieke __construct() en intro() metodes van die Fruit klas kan gebruik as gevolg van oorerwing.

Die Strawberry-klas het ook sy eie metode: message().



PHP - Oorerwing en die Beskermde Toegangswysiger

In die vorige hoofstuk het ons geleer dat protectedtoegang tot eienskappe of metodes binne die klas en deur klasse afgelei van daardie klas verkry kan word. Wat beteken dit?

Kom ons kyk na 'n voorbeeld:

Voorbeeld

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
  }
}

// Try to call all three methods from outside class
$strawberry = new Strawberry("Strawberry", "red");  // OK. __construct() is public
$strawberry->message(); // OK. message() is public
$strawberry->intro(); // ERROR. intro() is protected
?>

In die voorbeeld hierbo sien ons dat as ons probeer om 'n protected metode (intro()) van buite die klas aan te roep, ons 'n fout sal ontvang. public metodes sal goed werk!

Kom ons kyk na nog 'n voorbeeld:

Voorbeeld

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public function message() {
    echo "Am I a fruit or a berry? ";
    // Call protected method from within derived class - OK
    $this -> intro();
  }
}

$strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
$strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
?>

In die voorbeeld hierbo sien ons dat alles goed werk! Dit is omdat ons die protected metode (intro()) van binne die afgeleide klas noem.


PHP - Oorheersing van oorgeërfde metodes

Oorgeërfde metodes kan oorheers word deur die metodes (gebruik dieselfde naam) in die kinderklas te herdefinieer.

Kyk na die voorbeeld hieronder. Die __construct() en intro() metodes in die kinderklas (Strawberry) sal die __construct() en intro() metodes in die ouerklas (Fruit) ignoreer:

Voorbeeld

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "The fruit is {$this->name} and the color is {$this->color}.";
  }
}

class Strawberry extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
  }
}

$strawberry = new Strawberry("Strawberry", "red", 50);
$strawberry->intro();
?>

PHP - Die finale sleutelwoord

Die final sleutelwoord kan gebruik word om klasoorerwing te voorkom of om metode-oorheersing te voorkom.

Die volgende voorbeeld wys hoe om klasoorerwing te voorkom:

Voorbeeld

<?php
final class Fruit {
  // some code
}

// will result in error
class Strawberry extends Fruit {
  // some code
}
?>

Die volgende voorbeeld wys hoe om metode-oortreding te voorkom:

Voorbeeld

<?php
class Fruit {
  final public function intro() {
    // some code
  }
}

class Strawberry extends Fruit {
  // will result in error
  public function intro() {
    // some code
  }
}
?>