Tutorials
OOP voor PHP 4 deel 1
Object Oriented Programming voor PHP 4
Pagina 1
Inleiding
Object georiënteerd programmeren,
Als beginner zul je OOP moeilijk doorgrond baar kunnen vinden maar het is
vaak de terminologie die het je lastig maakt en niet de essentie van OOP.
Probeer de moeite te nemen waarbij ik in deze tutorial zo goed als mogelijk
uit zal leggen wat de termen betekenen en hoe je dat moet zien zodat het
beter te begrijpen is.
OOP kan een positieve invloed op je hebben, Je gaat anders leren denken,
programmeren en uiteindelijk tijd besparen waardoor je effectiever kan
werken.
[color=green]Goed, Mijn OOP tutorial deel 1 voor php4 richt zich eerst op een paar basis
zaken waarbij ik per onderdeel een eenvoudige voorbeeld geef. Deze
voorbeelden zijn niet altijd concreet genoeg om de kracht aan te duiden maar
zijn bedoeld om de uitleg te verklaren.[/color]
[color=red]Pagina is aangepast, bepaalde dingen die hier stonden verdienen een betere uitleg die ik aanhaal in Deel 2[/color]
Als beginner zul je OOP moeilijk doorgrond baar kunnen vinden maar het is
vaak de terminologie die het je lastig maakt en niet de essentie van OOP.
Probeer de moeite te nemen waarbij ik in deze tutorial zo goed als mogelijk
uit zal leggen wat de termen betekenen en hoe je dat moet zien zodat het
beter te begrijpen is.
OOP kan een positieve invloed op je hebben, Je gaat anders leren denken,
programmeren en uiteindelijk tijd besparen waardoor je effectiever kan
werken.
[color=green]Goed, Mijn OOP tutorial deel 1 voor php4 richt zich eerst op een paar basis
zaken waarbij ik per onderdeel een eenvoudige voorbeeld geef. Deze
voorbeelden zijn niet altijd concreet genoeg om de kracht aan te duiden maar
zijn bedoeld om de uitleg te verklaren.[/color]
[color=red]Pagina is aangepast, bepaalde dingen die hier stonden verdienen een betere uitleg die ik aanhaal in Deel 2[/color]
Pagina 2
Wat is Classification
Dit is het aanmaken van een Class met de benodigde attributen
[color=green]([/color]lees: gegevens[color=green])[/color] en methodes [color=green]([/color]lees: functies[color=green])[/color]
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
?>
We hebben hiermee het volgende bereikt:
1. Er is éen class gemaakt met de naam File_General ofwel:
[color=green]class[/color] [color=blue]File_General[/color] [color=green]{ }[/color]
2. Er is éen attribuut ofwel gegeven aangemaakt:
[color=green]var[/color] [color=blue]$m_sDefaultSystemAccessMode[/color][color=green];[/color]
3. Er is éen methode ofwel functie aangemaakt:
[color=green]function[/color] [color=blue]Check_If_Exist[/color][color=green]()[/color]
[color=green]([/color]lees: gegevens[color=green])[/color] en methodes [color=green]([/color]lees: functies[color=green])[/color]
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
?>
We hebben hiermee het volgende bereikt:
1. Er is éen class gemaakt met de naam File_General ofwel:
[color=green]class[/color] [color=blue]File_General[/color] [color=green]{ }[/color]
2. Er is éen attribuut ofwel gegeven aangemaakt:
[color=green]var[/color] [color=blue]$m_sDefaultSystemAccessMode[/color][color=green];[/color]
3. Er is éen methode ofwel functie aangemaakt:
[color=green]function[/color] [color=blue]Check_If_Exist[/color][color=green]()[/color]
Pagina 3
Wat is een Instantiation
Een instantiation ofwel instantie is niet meer dan ervoor zorgen dat de Class
kan worden gebruikt via een Object waar je mee kan communiceren om zo
informatie te verwerken en functies aan te roepen.
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
// Instantiation, Creating an instance of the class.
$o_File = new File_General();
?>
Door een [color=blue]variabele[/color] de opdracht [color=green]new[/color] te geven met daarachter de Class naam
wordt deze een Object en zal alle aanwezige kenmerken van de Class bevatten
van zowel Class naam, functies en de op dat moment aanwezige gegevens.
kan worden gebruikt via een Object waar je mee kan communiceren om zo
informatie te verwerken en functies aan te roepen.
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
// Instantiation, Creating an instance of the class.
$o_File = new File_General();
?>
Door een [color=blue]variabele[/color] de opdracht [color=green]new[/color] te geven met daarachter de Class naam
wordt deze een Object en zal alle aanwezige kenmerken van de Class bevatten
van zowel Class naam, functies en de op dat moment aanwezige gegevens.
Pagina 4
Wat is Inheritance
Inheritance is niet meer dan eigenschappen overerven van een andere class
en komt neer op het idee dat je als child (kind) de karakter en/of eigen-
schappen overneemt van je parent (ouder) waardoor je als child (kind) de
mogelijkheden krijgt om de functie's van de parent (ouder) te gebruiken.
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
Class File_Pictures extends File_General
{
var $m_sDefaultSystemAccessMode= '0644'; // Default system access mode : Everyone read, Owner read & write.
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFilename)
{
// Not implemented.
}
}
?>
Zoals je dan ziet hebben we een nieuwe [color=green]Class[/color] [color=blue]File_Pictures[/color] aangemaakt die
middels de koppeling [color=green]extends[/color] zich verbindt naar de [color=green]Class [/color][color=blue]File_General[/color]
waarmee we dan een relatie tussen elkaar leggen[color=green]([/color]lees: verkrijgen[color=green])[/color].
Het belang van de relatie tussen elkaar is dat de Class [color=green]File_General[/color] de basis zal behoren te doen om in het algemeen met bestanden te werken ongeacht het type bestand.
De Class [color=green]File_Pictures[/color] heeft de taak om zich te richten op bestanden die alleen maar van een grafische formaat zijn zoals jpg, gif en png.
en komt neer op het idee dat je als child (kind) de karakter en/of eigen-
schappen overneemt van je parent (ouder) waardoor je als child (kind) de
mogelijkheden krijgt om de functie's van de parent (ouder) te gebruiken.
Voorbeeld:
<?php
class File_General
{
var $m_sDefaultSystemAccessMode = '0600'; // Default system access mode : Owner read & write.
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Not implemented.
}
}
Class File_Pictures extends File_General
{
var $m_sDefaultSystemAccessMode= '0644'; // Default system access mode : Everyone read, Owner read & write.
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFilename)
{
// Not implemented.
}
}
?>
Zoals je dan ziet hebben we een nieuwe [color=green]Class[/color] [color=blue]File_Pictures[/color] aangemaakt die
middels de koppeling [color=green]extends[/color] zich verbindt naar de [color=green]Class [/color][color=blue]File_General[/color]
waarmee we dan een relatie tussen elkaar leggen[color=green]([/color]lees: verkrijgen[color=green])[/color].
Het belang van de relatie tussen elkaar is dat de Class [color=green]File_General[/color] de basis zal behoren te doen om in het algemeen met bestanden te werken ongeacht het type bestand.
De Class [color=green]File_Pictures[/color] heeft de taak om zich te richten op bestanden die alleen maar van een grafische formaat zijn zoals jpg, gif en png.
Pagina 5
Wat is Parent
Een [color=green]parent [/color]in OOP is een begrip en tevens een Scope resolution operator.
Als begrip wil men hiermee zeggen dat een Class een [color=green]extends[/color] is naar een andere
class waarmee we dus een relatie leggen tussen twee of meerdere classes.
Voorbeeld #1:
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// not implemented.
}
}
?>
Vanwege de [color=green]extends[/color] geven we aan dat de [color=green]Class[/color] [color=blue]File_Pictures[/color] zich moet gedragen
als child [color=green]([/color]lees: kind van[color=green])[/color] waardoor de [color=green]Class[/color] [color=blue]File_General[/color] de [color=green]parent[/color] is van
de [color=green]Class[/color] [color=blue]File_Pictures[/color]
[hr]
Tevens heeft [color=green]Parent[/color] ook nog een andere betekenis en dit als methode
[color=green]([/color]lees: functie[color=green])[/color] in de class zelf als een Scope resolution operator:
Voorbeeld #2:
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// (if picture then
parent::Check_If_Exist($p_sFilePath,$p_sFileName);
// endif)
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Zoals je ziet staat in de [color=green]Class[/color] [color=blue]File_Pictures[/color] een functie genaamd [color=blue]Check_If_Picture[/color][color=green]()[/color] welke dus weer een andere functie aanroept [color=blue]parent[/color][color=green]::[/color][color=blue]Check_If_Exist[/color][color=green]()[/color] die zich bevindt in de [color=green]Class[/color] [color=blue]File_General[/color].
Met [color=blue]parent[/color][color=green]::[/color] geef je dus aan dat je een functie wilt aanspreken die in de [color=green]extends[/color] [color=green]([/color]lees: verlenging[color=green])[/color] ligt van de Class waarvan je een functie wilt aanroepen.
[hr]
Je zult ook merken dat [color=blue]$this[/color][color=green]->[/color][color=blue]Check_If_Exist[/color][color=green]()[/color] ook wel werkt maar daar geef je niet direkt mee aan dat je perse een functie aanspreekt in de verlenging van de Class maar in de gehele keten van beide Classes en dan de eerste die dan wordt gevonden zal aanspreken.
Voorbeeld #3: Zo gaat het fout
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'I seem to made a mistake';
}
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// (if picture then
$this->Check_If_Exist($p_sFilePath,$p_sFileName);
// endif)
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Zoals je ziet hebben beiden Classes een zelfde functie [color=blue]Check_If_Picture[/color][color=green]()[/color] maar beiden met een heel ander resultaat waarbij je met de [color=blue]$this[/color] methode uitkomt op de functie binnen de Class zelf.
Als begrip wil men hiermee zeggen dat een Class een [color=green]extends[/color] is naar een andere
class waarmee we dus een relatie leggen tussen twee of meerdere classes.
Voorbeeld #1:
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// not implemented.
}
}
?>
Vanwege de [color=green]extends[/color] geven we aan dat de [color=green]Class[/color] [color=blue]File_Pictures[/color] zich moet gedragen
als child [color=green]([/color]lees: kind van[color=green])[/color] waardoor de [color=green]Class[/color] [color=blue]File_General[/color] de [color=green]parent[/color] is van
de [color=green]Class[/color] [color=blue]File_Pictures[/color]
[hr]
Tevens heeft [color=green]Parent[/color] ook nog een andere betekenis en dit als methode
[color=green]([/color]lees: functie[color=green])[/color] in de class zelf als een Scope resolution operator:
Voorbeeld #2:
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// (if picture then
parent::Check_If_Exist($p_sFilePath,$p_sFileName);
// endif)
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Zoals je ziet staat in de [color=green]Class[/color] [color=blue]File_Pictures[/color] een functie genaamd [color=blue]Check_If_Picture[/color][color=green]()[/color] welke dus weer een andere functie aanroept [color=blue]parent[/color][color=green]::[/color][color=blue]Check_If_Exist[/color][color=green]()[/color] die zich bevindt in de [color=green]Class[/color] [color=blue]File_General[/color].
Met [color=blue]parent[/color][color=green]::[/color] geef je dus aan dat je een functie wilt aanspreken die in de [color=green]extends[/color] [color=green]([/color]lees: verlenging[color=green])[/color] ligt van de Class waarvan je een functie wilt aanroepen.
[hr]
Je zult ook merken dat [color=blue]$this[/color][color=green]->[/color][color=blue]Check_If_Exist[/color][color=green]()[/color] ook wel werkt maar daar geef je niet direkt mee aan dat je perse een functie aanspreekt in de verlenging van de Class maar in de gehele keten van beide Classes en dan de eerste die dan wordt gevonden zal aanspreken.
Voorbeeld #3: Zo gaat het fout
<?php
class File_General
{
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$p_sFilePath.' File: '.$p_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
function Check_If_Exist($p_sFilePath='',$p_sFileName='')
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'I seem to made a mistake';
}
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
// (if picture then
$this->Check_If_Exist($p_sFilePath,$p_sFileName);
// endif)
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Zoals je ziet hebben beiden Classes een zelfde functie [color=blue]Check_If_Picture[/color][color=green]()[/color] maar beiden met een heel ander resultaat waarbij je met de [color=blue]$this[/color] methode uitkomt op de functie binnen de Class zelf.
Pagina 6
Wat is $this
[color=blue]$this[/color] is een methode om binnen de Class of [color=green]extends[/color] van Classes te werken.
Voorbeeld #1: Binnen de class zelf
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
$this->Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_Pictures class.
$my_general_file = new File_General();
// Using a function of the File_Pictures class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Zoals je ziet als de functie [color=blue]Check_If_Exist[/color][color=green]()[/color] wordt aangesproken met een aantal parameters op [color=purple]regel 35[/color] dan zal deze in de class zelf de parameters opslaan in het object en dan een andere functie genaamd [color=blue]Just_For_Demonstrating[/color][color=green]()[/color] aanroepen die dan ter demonstratie de verkregen gegevens zal vertonen.
[hr]
Voorbeeld #2: Vanuit de [color=green]extends[/color] class
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
$this->Check_If_Exist();
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Nu spreken we een methode aan vanuit de extended Class met [color=blue]$this[/color][color=green]->[/color]Check_If_Picture[color=green]()[/color] die dan de parameters opslaat in het Object,
Vervolgens een functie [color=blue]$this[/color][color=green]->[/color]Check_If_Exist[color=green]()[/color] aanspreekt die in de
[color=green]extends[/color] Class File_General staat.
Opmerking:
Indien dezelfde functie in beide Classes voorkomt zal het aanspreken
van een functie via [color=blue]$this[/color] leiden tot onverwachte resultaten
als je daar geen rekening mee houdt.
In zon geval is het beter om concreet te zijn welke functie in welke class je wil aanspreken door gebruik te maken van een Scope resolution operator.
Zo ongewenste situatie staat in voorbeeld #3 van Sectie: Wat is Parent
Voorbeeld #1: Binnen de class zelf
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
$this->Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_Pictures class.
$my_general_file = new File_General();
// Using a function of the File_Pictures class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Zoals je ziet als de functie [color=blue]Check_If_Exist[/color][color=green]()[/color] wordt aangesproken met een aantal parameters op [color=purple]regel 35[/color] dan zal deze in de class zelf de parameters opslaan in het object en dan een andere functie genaamd [color=blue]Just_For_Demonstrating[/color][color=green]()[/color] aanroepen die dan ter demonstratie de verkregen gegevens zal vertonen.
[hr]
Voorbeeld #2: Vanuit de [color=green]extends[/color] class
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
Class File_Pictures extends File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if the file is indeed a picture.
function Check_If_Picture($p_sFilePath,$p_sFileName)
{
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
$this->Check_If_Exist();
}
}
// Creating an instance of the File_Pictures class.
$my_picture_file = new File_Pictures();
// Using a function of the File_Pictures class
$my_picture_file->Check_If_Picture('Map','foto.jpg');
?>
Nu spreken we een methode aan vanuit de extended Class met [color=blue]$this[/color][color=green]->[/color]Check_If_Picture[color=green]()[/color] die dan de parameters opslaat in het Object,
Vervolgens een functie [color=blue]$this[/color][color=green]->[/color]Check_If_Exist[color=green]()[/color] aanspreekt die in de
[color=green]extends[/color] Class File_General staat.
Opmerking:
Indien dezelfde functie in beide Classes voorkomt zal het aanspreken
van een functie via [color=blue]$this[/color] leiden tot onverwachte resultaten
als je daar geen rekening mee houdt.
In zon geval is het beter om concreet te zijn welke functie in welke class je wil aanspreken door gebruik te maken van een Scope resolution operator.
Zo ongewenste situatie staat in voorbeeld #3 van Sectie: Wat is Parent
Pagina 7
Wat is Self
[color=green]Self::[/color] is niet een van de Scope resolution operators die je kan gebruiken in Php4 maar wil ik toch even dit onderwerp aanhalen zodat je weet wat dit inhoudt en hoe het zelfde effect kan worden bereikt.
We moeten een alternatieve wijze gaan gebruiken en dat is gebruik maken van een Scope resolution operators waarbij we de naam van de Class zelf gebruiken.
Voorbeeld:
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_Pictures class.
$my_general_file = new File_General();
// Using a function of the File_Pictures class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Op deze manier zal [color=blue]File_General[/color][color=green]::[/color][color=blue]Just_For_Demonstrating[/color][color=green]()[/color] de functie alleen binnen de Class zelf aanspreken ongeacht of er nog meerdere classes zijn die middels [color=green]extends[/color] in elkaars verlenging liggen.
We moeten een alternatieve wijze gaan gebruiken en dat is gebruik maken van een Scope resolution operators waarbij we de naam van de Class zelf gebruiken.
Voorbeeld:
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_Pictures class.
$my_general_file = new File_General();
// Using a function of the File_Pictures class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Op deze manier zal [color=blue]File_General[/color][color=green]::[/color][color=blue]Just_For_Demonstrating[/color][color=green]()[/color] de functie alleen binnen de Class zelf aanspreken ongeacht of er nog meerdere classes zijn die middels [color=green]extends[/color] in elkaars verlenging liggen.
Pagina 8
Wat is een Constructor
Er zijn uitzonderingen maar deze terzijde gelaten is een Constructor niet meer
dan een functie binnen de Class die automatisch wordt uitgevoerd als er
een Instantie wordt gemaakt van de Class. Deze Constructor (functie) moet
dan dezelfde naam hebben als de Class.
Voorbeeld:
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function File_General($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_General class.
$my_general_file = new File_General('Map','foto.jpg');
?>
Zoals je ziet wordt op [color=purple]regel 30[/color] een Instantie aangemaakt van de class File_General waarbij we een referentie krijgen naar het Object $my_general_file maar tevens geven we direct parameters door die door een functie in de Class zelf gelijk wordt afgehandeld.
Dit komt dus door dat de functie naam gelijk is aan de naam van de Class zelf
waardoor deze methode handig is om bij het Instantieren van de Class automatisch benodigde handelingen te laten verrichten.
dan een functie binnen de Class die automatisch wordt uitgevoerd als er
een Instantie wordt gemaakt van de Class. Deze Constructor (functie) moet
dan dezelfde naam hebben als de Class.
Voorbeeld:
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function File_General($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_General class.
$my_general_file = new File_General('Map','foto.jpg');
?>
Zoals je ziet wordt op [color=purple]regel 30[/color] een Instantie aangemaakt van de class File_General waarbij we een referentie krijgen naar het Object $my_general_file maar tevens geven we direct parameters door die door een functie in de Class zelf gelijk wordt afgehandeld.
Dit komt dus door dat de functie naam gelijk is aan de naam van de Class zelf
waardoor deze methode handig is om bij het Instantieren van de Class automatisch benodigde handelingen te laten verrichten.
Pagina 9
Wat is een Scope resolution operator
Een scope resolution operator richt zich op methode's waarbij je rechtstreeks
een functie benaderd en het kan zowel binnen als buiten de Class zijn.
Mogelijk scope resolution operators voor php4 zijn:
[color=blue]parrent[/color][color=green]::[/color]<functie>
<Class naam>[color=green]::[/color]<functie>
Voorbeeld #1: Binnen de class zelf
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_General class.
$my_general_file = new File_General();
// Using a function of the File_General class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Zoals je ziet op [color=purple]regel 16[/color] staat er [color=blue]File_Genera[/color]l[color=green]::[/color][color=blue]Just_For_Demonstrating[/color][color=green]()[/color] waarbij de Scope operator zich richt op een functie in de Class zelf gezien de naam voor de Scope aanduiding [color=green]::[/color] gelijk staat aan de naam van de Class zelf.
[hr]
Voorbeeld #2: Buiten de class
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Using a scope operator without having an Instance refering to an object.
File_General::Check_If_Exist('Map','foto.jpg');
?>
Op [color=purple]regel 30[/color] staat dus een methode om via een scope resolution operator een functie van een Class aan te roepen zonder dat je van te voren een Instantie van de Class hebt gemaakt.
[hr]
Voor meer informatie over de [color=blue]parent[/color][color=green]::[/color] scope resolution operator zie de
sectie: Wat is Parent
een functie benaderd en het kan zowel binnen als buiten de Class zijn.
Mogelijk scope resolution operators voor php4 zijn:
[color=blue]parrent[/color][color=green]::[/color]<functie>
<Class naam>[color=green]::[/color]<functie>
Voorbeeld #1: Binnen de class zelf
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Creating an instance of the File_General class.
$my_general_file = new File_General();
// Using a function of the File_General class
$my_general_file->Check_If_Exist('Map','foto.jpg');
?>
Zoals je ziet op [color=purple]regel 16[/color] staat er [color=blue]File_Genera[/color]l[color=green]::[/color][color=blue]Just_For_Demonstrating[/color][color=green]()[/color] waarbij de Scope operator zich richt op een functie in de Class zelf gezien de naam voor de Scope aanduiding [color=green]::[/color] gelijk staat aan de naam van de Class zelf.
[hr]
Voorbeeld #2: Buiten de class
<?php
class File_General
{
var $m_sFilePath;
var $m_sFileName;
// Function to check if a file exists's.
function Check_If_Exist($p_sFilePath,$p_sFileName)
{
// Storing parameter data into $this (object of the class).
$this->m_sFilePath = $p_sFilePath;
$this->m_sFileName = $p_sFileName;
// Executing function in this class.
File_General::Just_For_Demonstrating();
}
function Just_For_Demonstrating()
{
// Normaly you dont use output to display information inside
// a class, its just here to show you the results if you try
// running this example for yourself.
echo 'Folder: '.$this->m_sFilePath.' File: '.$this->m_sFileName.'<br />';
}
}
// Using a scope operator without having an Instance refering to an object.
File_General::Check_If_Exist('Map','foto.jpg');
?>
Op [color=purple]regel 30[/color] staat dus een methode om via een scope resolution operator een functie van een Class aan te roepen zonder dat je van te voren een Instantie van de Class hebt gemaakt.
[hr]
Voor meer informatie over de [color=blue]parent[/color][color=green]::[/color] scope resolution operator zie de
sectie: Wat is Parent
Reacties
0