Tutorials

Visibility

Alles over public, protected, en private vars in classes

Pagina 1

Voorwoord

<?php
function newline($num = 1, $return = false) {
$to_return = "";
if ($num < 1) {
$num = 1;
}
while ($num != 0) {
$num--;
if ($return == true) {
$to_return .= "\n";
} elseif ($return == false) {
echo "\n";
} else {
$num++;
echo "Error:\nReturn = {$return}\nNum = {$num}";
break;
}
}
if ($return == true) {
return $to_return;
}
}
header("Content-type: text/plain");
?>
Je moet net doen of het bovenstaande script altijd bovenaan in mijn tutorialscripts staat, zodat je weet dat als ik newline(2); doet, dat je het weet waar ik heb over hebt.

Voorwoord
Deze tutorial gaat over PHP 5's visibility. Ja. Ook iets nieuws in PHP 5. Omdat dit een uitgebreid onderwerp is, heb ik het niet aan mijn PHP 5 tutorial toegevoegt.
Pagina 2

Syntax

Intro
Visibility is vertaald zichtbaarheid en dat kan bepalen hoe de reikwijde(scope) van een variable of functie is. En dit heeft ook een bepaald syntax, jawel. De syntax is als volgt:
<?php
class Klassenaam {
zichtbaarheid $variablenaam = 'waarde';

zichtbaarheid function functienaam($argumenten...) {
functiecode
}
}
?>

Zichtbaarheid kan public, protected of private zijn. Volgende pagina daarover meer.

Visibility van Variablen
Je kan in php 5 dus geen public var $hallo doen, dan krijg je namelijk de volgende error:
Parse error: parse error, unexpected T_VAR, expecting T_VARIABLE in H:\Server\Apache2\htdocs\class\class.php on line 29

Dus dat betekent dat je voortaan var $hallo; moet gaan handhaven of public $hello;
De combinatie van beiden is dus geen succes.

Visibility van functies
Ook functies kunnen een visibility hebben. Deze werkt hetzelfde als die van de variablen.
Pagina 3

Public, protected, private

Public
Public is niets anders als publiek. Een public variable
kan je overal en altijd zien in een class.
Als je van een class variable of functie niet defineerd of hij public etc. is,
dan wordt ie als public beschouwd.
<?php
class mijn_public {
public $vari = 'Hallow';

public function printvari() {
echo $this->vari;
newline(1);
}

public function printhallo() {
$this->printvari();
}
}

$obj = new mijn_public;
$obj->printvari();
$obj->printhallo();
?>
Dit zal werken zoals verwacht en zal
Hallow
Hallow
printen.

Private
Private variablen en functies kan je niet buiten de class gebruiken. Alleen in de class zelf, of in de class die de huidige extends.
<?php
class mijn_public {
private $vari = 'Hallow';

private function printvari() {
echo $this->vari;
newline(1);
}

public function printhallo() {
$this->printvari();
}
}

$obj = new mijn_public;
//$obj->printvari(); //zal niet werken: Fatal error: Call to private method mijn_public::printvari() from context '' in H:\Server\Apache2\htdocs\class\class.php on line 42
$obj->printhallo(); //dit werkt zoals verwacht en zal "Hallow" printen
?>

Protected
Protected is eigenlijk de meest "egoistische" vorm. Protected variablen en functies kunnen alleen gebruikt worden de class die ze gedefineerd heeft.

<?php
class mijn_public {
protected $vari = 'Hallow';

protected function printvari() {
echo $this->vari;
newline(1);
}

public function printhallo() {
$this->printvari();
}
}

class mijn_tweede {
protected $var2 = 'XXXX';

protected function tsja() {
//$this->printvari(); //zal niet werken en resulteren in een fatal error
}
}
?>

Extending
Bedenk wel dat dit altijd nog mogelijk is. Ondanks je protected ofzoiets hebt gegeven. Dus wil je dat niet, gebruik dan het Final keyword!

Reacties

0
Nog geen reacties.