Cursussen/Courses Codesnippets     Top 
PHP - Classes and objects


1. Concepts
Class:
A class is a blueprint of the structure of an object.
The class is created with the keyword 'class'.
The class contains properties (variables) and methods (functions).
The constructor method contains program code to give the properties a value.
The other methods contain program code to perform specific operations.
Object:
An object is an instance of a class (it belongs to a class).
Multiple objects of the same class can exist.
An object is created with the keyword 'new'.
Class definition:
To describe a class, choose a suitable name for the class (eg person, database, category, ...).
You also choose names for the properties (eg name, first name, gender, get_name, set_name, get_records, ...). For a class that corresponds to a table in a database, it is best to make the names of the properties the same as the field names in the table. For the methods you also choose suitable names and you also provide the function arguments of the method (eg show_properties(), set_name($nm), search_record($id), ...).
The constructor method has the fixed name '__construct()' (2 times underscore and the keyword construct). You indicate the accessibility of the properties (the scope) with 1 of the keywords public, private or protected.
<?php
// persoon.php
// persoon klasse (abstracte klasse, basis klasse)

class persoon {
	// eigenschappen (voor een basisklasse scope = protected)
	protected $naam;
	protected $voornaam;
	protected $geslacht;
	protected $telefoon;
	
	// methodes
	public function __construct($nm = "", $vm = "", $gl = "", $tel = "") {
		// constructor met standaard lege eigenschappen (= "")
		$this->naam = $nm;
		$this->voornaam = $vm;
		$this->geslacht = $gl;
		$this->telefoon = $tel;
	}
	public function toon_eigenschappen() {
		$meld = "Naam: " . $this->naam . "<br>";
		$meld .= "Voornaam: " . $this->voornaam . "<br>";
		$meld .= "Geslacht: " . $this->geslacht . "<br>";
		$meld .= "Telefoon: " . $this->telefoon . "<br>";
		echo $meld . "<br>";
	}
}


2. Constructor
A constructor is a method of the class that is executed when an object of that class is created. In the constructor method, the properties of the object are given a (default) value.
The scope of this method is always public.
The following code runs the constructor:
require "persoon.php";
$pers1 = new persoon("Jan","Janssens","M","0123456789");
public function __construct($nm = "", $vm = "", $gl = "", $tel = "") {
	// constructor met standaard lege eigenschappen (= "")
	$this->naam = $nm;
	$this->voornaam = $vm;
	$this->geslacht = $gl;
	$this->telefoon = $tel;
}


3. Methods
The properties of a class are "private" by default, so you cannot change the value of those properties from outside the class.
The constructor initializes the properties.
If you still want to change (or request) the value of the properties, you have to write a get or a set function.
An example:
require "leerling.php";
$lln1 = new leerling("Ria","Peeters","V","13246546" ,"5AIT","PeetersR");
echo $lln1->get_gebruikersnaam() ."";
In the 'codesnippets' section of this website you can find other examples of classes such as a database class, a table class, etc...
<?php
class leerling {
   ...
public function get_gebruikersnaam() {
	return $this->gebruikersnaam;
}
   ...
}


4. Inheritance
Inheritance is a way of turning an existing class into a new class with the same properties and methods as the existing class. In addition, the new class can also have its own properties and methods.
This method promotes the reuse of existing program code.
When analyzing you can find similarities in some base classes that lead to the creation of a base class (= generalization).
You can make different types of derived classes from a base class (= specialization).
A derived class is indicated by the keyword 'extends' followed by the name of the base class.
In the constructor of the derived class you can execute the constructor of the base class with the call 'parent::__construct( arguments ).
Creating an object of the derived class is then done as follows:
require "persoon.php";	// basisklasse definitie nodig
require "leerling.php";
echo "Een object van de klasse leerling:" . "";
$lln1 = new leerling("Ria","Peeters","V","13246546" ,"5AIT","PeetersR");
$lln1->toon_eigenschappen();
echo $lln1->get_gebruikersnaam() ."";
<?php
// leerling.php
// leerling klasse (erft de eigenschappen van persoon)

class leerling extends persoon {
	// eigenschappen (scope = private)
	private $klas;
	private $gebruikersnaam;
	
	// methodes
	public function __construct($nm = "", $vm = "", $gl = "", $tel = "", $kl = "", $geb = "") {
		// persoon eigenschappen initialiseren
		parent::__construct($nm, $vm, $gl, $tel);
		$this->klas = $kl;
		$this->gebruikersnaam = $geb;
	}
	public function toon_eigenschappen() {
		$meld = "Naam: " . $this->naam . "<br>";
		$meld .= "Voornaam: " . $this->voornaam . "<br>";
		$meld .= "Geslacht: " . $this->geslacht . "<br>";
		$meld .= "Telefoon: " . $this->telefoon . "<br>";
		$meld .= "Klas: " . $this->klas . "<br>";
		$meld .= "Gebruikersnaam: " . $this->gebruikersnaam . "<br>";
		echo $meld . "<br>";
	}
	public function get_gebruikersnaam() {
		return $this->gebruikersnaam;
	}
	
}