In this article, I'll explain how to use classes in PHP 5. So, let's start!
Every class definition begins with the keyword "class", followed by a class name, followed by a pair of curly braces which enclose the definitions of the class' properties and methods.
The class name can be any valid label which is a not a PHP reserved word (like public, private, addslashes, etc.). A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
A class may contain its own constants, variables (properties), and functions (methods).
The keyword $this is available in all classes. When you use the "get_class($this)", you will get the name of the class name:
The output of this will be: myClass
Well, let's say we include the file class.myclass.php (see below) in index.php (below too), where we want to use the class' methods and properties:
File: class.myclass.php
File: index.php
The output of this will be:
myVar
myVar
myVar
Let's take a closer look at the code:
First, we include the class file. If we wouldn't do that, PHP gives an error that "myClass" is not defined.
Then, we create a new instance of the class "myClass". If you don't set a new instance and just use myClass::getmyVar(), the script won't work correctly because "$this" is not defined.
With the just created instance of "myClass", we can use the property "myVar" and the methods "myFunction()" and "getmyVar()". The difference between the methods is that "myFunction()" echoes the variable "myVar" immediately and "getmyVar()" just returns "myVar".
What about inheriting a class?
That goes with the keyword "extends". Let's take a look at the following example:
This does exactly the same as the example with "index.php", but now the class "childClass" extends "myClass", what means it inherits it's methods, properties, etc. However, if the function "getmyVar" in class "myClass" was declared with the keyword "private" instead of "public", the script won't work. Private functions are not visible for an inherited class.
You can also define "constants" in PHP classes. Constants always remain unchangeable Here's an example:
Well, let's see how constructors/destructors work in the classes:
This will echo "Class constructed", because you defined a new class. When the class gets destructed (when exit() or something is called), the script will output "Class destructed". Of course, you can pass parameters with __construct and __destruct, as they work as normal methods.
Well, this are the very basics of classes in PHP. I hope you have learned something about this!
Every class definition begins with the keyword "class", followed by a class name, followed by a pair of curly braces which enclose the definitions of the class' properties and methods.
The class name can be any valid label which is a not a PHP reserved word (like public, private, addslashes, etc.). A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
A class may contain its own constants, variables (properties), and functions (methods).
PHP Code:
<?php
class myClass {
public $myVar = "myVar";
public function myFunction() {
echo $this->myVar;
}
}
?>
PHP Code:
<?php
class myClass {
function getClassName() {
echo get_class($this);
}
}
?>
Well, let's say we include the file class.myclass.php (see below) in index.php (below too), where we want to use the class' methods and properties:
File: class.myclass.php
PHP Code:
<?php
class myClass {
public $myVar = "myVar";
public function myFunction() {
echo $this->myVar;
}
public function getmyVar() {
return $this->myVar;
}
}
?>
PHP Code:
<?php
include('class.myclass.php');
$instance = new myClass();
echo $instance->myVar . "<br>";
$instance->myFunction() . "<br>";
echo $instance->getmyVar();
?>
myVar
myVar
myVar
Let's take a closer look at the code:
First, we include the class file. If we wouldn't do that, PHP gives an error that "myClass" is not defined.
Then, we create a new instance of the class "myClass". If you don't set a new instance and just use myClass::getmyVar(), the script won't work correctly because "$this" is not defined.
With the just created instance of "myClass", we can use the property "myVar" and the methods "myFunction()" and "getmyVar()". The difference between the methods is that "myFunction()" echoes the variable "myVar" immediately and "getmyVar()" just returns "myVar".
What about inheriting a class?
That goes with the keyword "extends". Let's take a look at the following example:
PHP Code:
<?php
include('class.myclass.php');
class childClass extends myClass {
public function echoMyVar() {
echo parent::myVar;
parent::myFunction();
echo parent::getmyVar();
}
}
$instance = new childClass();
$instance->echoMyVar();
?>
You can also define "constants" in PHP classes. Constants always remain unchangeable Here's an example:
PHP Code:
<?php
class constExample {
const constVar = "constant value";
public function echoConst() {
echo $this->constVar;
}
}
$inst = new constExample();
$inst->echoConst();
?>
PHP Code:
<?php
class baseClass {
function __construct() {
echo "Class constructed";
}
function __destruct() {
echo "Class destructed";
}
}
$inst = new baseClass();
?>
Well, this are the very basics of classes in PHP. I hope you have learned something about this!