There are three main concepts here: Objects, classes and namespaces.
A class is a group of common properties discribing something, eg a car. All cars have wheels, an engine, a steering wheel etc.
Class car
{
private $engine;
private $wheels;
private $seats;
private $steeringwheel;
private $paintcolour;
}
These properties are common to all cars.
Some cars are made by a different company, but they still have the same base properties defined in our car class, so we can inherit those properties into a new class:
Class Mazda extends car
{
private $radialengine;
}
Here our Mazda class has all the properties of car, but it also has the $radialengine property.
Because cars are all different, they have different paint colours, different options etc, we are going to end up with hundreds of classes. So, instead, we create objects, which are like copies of a class, that have their own values for each of the different properties:
$mx5 = new Mazda;
$mx5->engine = 2.0;
$mx5->wheels = 4;
$mx5->seats = 2;
$mx5->steeringwheel = yes;
$mx5->paintcolour = red;
$mx5->radialengine = yes;
We could also create an RX7 object, that would also be a mazda, but its properties would be completely independent of the mx5 object.
Classes can also define methods, which are like functions to do specific actions. For instance, a car class would have a start() method to start the engine.
Class car
{
private $engine;
private $wheels;
private $seats;
private $steeringwheel;
private $paintcolour;
Public function start()
{
start engine code here
}
}
methods can be public (accessible to all other code) or private (accessible only to other code in the same class).
A Namespace is simply a group of classes. e.g
namespace vehicle
{
class car
{
....
}
class train
{
....
}
}