<?php
# person.class
class Person{
var $name; # this is a property
function set_name($new_name){ # this is a method
$this->name=$new_name;
}
function get_name(){ # this is a method
echo $this->name;
}
}
?>
<?php
include("person.class");
$person=new person();
$person->set_name("james");
$person->get_name();
?>
Constructors
When an object is created, these variables are instantiated upon object creation. I.e. the object is created (instantiated) and the object properties are initialized at the same time.
<?php
# person.class
class Person{
function __construct($name){ # this is a method
$this->name=$name;
}
function change_name ($changed_name){
$this->name=$changed_name;
}
function get_name(){ # this is a method
echo $this->name;
}
}
?>
<?php
include("person.class");
$person=new person("emma");
$person->get_name();
$person->change_name("dennis");
$person->get_name();
?>
Restricting access to properties using access modifiers
You can restrict access to class properties (the variables) using these 3 access modifiers:
- Public – this is a default modifier
- Protected – only the same class and classes derived from that class can access the property
- Private – only the same class can access the property
What does this mean in practice?
When a property is declared public
<?php
# person.class
class Person{
public $name; # this does need to be declared as it's public by default
function __construct($name){ # this is a method
$this->name=$name;
}
function change_name ($changed_name){
$this->name=$changed_name;
}
function get_name(){ # this is a method
echo $this->name. "<br>";
}
}
?>
<?php
include("person.class");
$person=new person("emma");
$person->get_name();
$person->change_name("dennis");
$person->get_name();
echo $person->name." accessed directly";
?>
In the above you can see that we can access the property ‘name’ directly, we don’t need a method (a function) within the class to access it.
When a property is declared private
If in the above code we change the line of the class:
public $name; # this does need to be declared as it's public by default
to
private $name;
We get the error:
This is because we cannot access the private property outside of the class itself. If the property were declared ‘protected’ we should be able to access it from class that is derived from this class through inheritance. We’ll look at that later.
Restricting access to methods using access modifiers
<?php
# person.class
class Person{
private $name;
function __construct($name){ # this is a method
$this->name=$name;
}
function change_name ($changed_name){
$this->name=$changed_name;
}
private function get_bank_number(){
return
$this->pinn_number;
}
function get_name(){ # this is a method
echo $this->name. "<br>";
}
}
?>
Notice the get_bank_number method is ‘private’. We cannot access this method outside of the class, so we will not be able to access it from a php page. It can ONLY be accessed from within this class (i.e. likely in another method that is within this class). Because it is private, is cannot be accessed either by a class that EXTENDS this class (using the ‘protected’ access modifier would allow this).
Inheritance
Allows the use of properties and methods in the base class. I.e. reuse the code written in the base class.
e.g.
class employee extends person
{
function __construct($employee_name) {
#code
}
}
class employee has access to all the public and protected properties and methods of the person class.