Skip to content

PHP Classes – An Overview

Reading Time: 2 minutes

PHP Classes – An Overview

By using PHP classes we can make our codes streamlined and the result is simply great.

Defining Classes
A class can be seen as a template or blueprint for an object. It includes the local functions and data that are the data type defined by the programmer.
A class can consist of some common and shared attributes and then extend into sub-classes from its parent class. These sub-classes consist of unique attributes in addition to having the features of their parent class. To create a new sub-class we need to add a constructor that will run to define certain specifics for that sub-class.

A typical class can be defined as:
<?php
class phpClass{
var $var1;
var $var2 = “constant string”;
function myfunc ($arg1, $arg2) {
[ .. ]
}
[ .. ]
}
?>

Privacy
Taking for example a class of say, Buildings that has a sub-class of Flat defined by specific attributes such as Kitchen and Bathroom
class Building {
private $residence = true

protected $kitchen = ‘’;
protected $bathroom = ‘’;
}
class Flat extends Building{
public function _construct(){
$this->kitchen = ‘modular’;
$this->bathroom = ‘tiled’;
}
}
Here, Buildings is a class which is extending into a sub-class of Flat where all the variables or attributes of the class Building are copied.
Note that the code given in the example consists of words ‘protected’, ‘public’ and ‘private’. These provide varying degrees of access control. While Public can be called anywhere be it in the, outside the class or in the sub-class; Protected and Private have restrictions. Protected can only be called from within the current class or from the sub-classes and Private can be called only by the containing class.

See also  Best Practices to Generate Application Key in Laravel 2022

Magic Method
Magic methods can be used when we try to get and set those variables which do not exist. These are generic functions like _get(), _set(), _call() and _callStatic().
Overloading or magic methods of get and set will not work on a variable that already exists. Also, they it will call the overloading methods if you try to use them on the variables that you do not have access to.
Example:
class Demo {
private $data = array(){

public function_set($variable, $value){
echo ‘Setting’ . $variable . ‘to’ . $value;
$this->data[$variable] = $value;
}
public function_get($variable){
if(isset($this->data[$variable])){
return $this->data[$variable];
}else{
die(‘Unknown variable.’);
}
}

}

$d = new Demo;

$d->test = ‘Test Variable’;

echo $d->test;

echo $d->testFail;
Call and callStatic have the same function with an exception that the latter is static methods while Call is not. They are used to call the functions that do not exist in the class.

Singletons
This is a class in PHP that is instantiated only once and never again. This is done using a static variable in the class, called instance, and a static method called as getInstance. This will enable checking for the existence of instance and if it doesn’t exist it will facilitate its creation and then return it.
Example:
class DemoSingleton {
private static $instance = NULL;

public static function getInstance(){
if(is_null(self: :$instance)){
self: :$instance = new self();
}
Return self: :$instance;

}
}
$class = DemoSingleton: :getInstance();
Autoloading
Autoload automatically loads a file in which the class is written, when you try to instantiate it. It is done using function spl_autoload_register().
Example:
spl_autoload_register(function($class){
include_once(‘includes/’ . $class . ‘.class.php’)
});

Leave a Reply