This paragraph is about the object oriented structure of PHsPeed. It can be confusing to novice developers to see what objects are and why they are important. If you want to become an experience developer then it is important to understand the concepts as OOP (object oriented programming) is mainstream in any programming language nowadays.


In OOP (i.e. object-oriented programming), you will combine codes and data to create an object. A computer application created using this style consists of different objects that can communicate with each other. Often, these objects are self-contained and possess different methods and properties. Properties serve as an object’s data. Thus, these are variables owned by the object they point to. The methods, on the other hand, are functions an object supports. Classes serve as templates for a programming object. They describe the properties and methods that an object will possess. You can see each object as an Island. The Island is self-contained and communicates with elements on other Islands. To move data between the Islands you use ferries, a formal way of transporting data. 

SPObject

In PHsPeed it all begins with a standard object (simplified):


    class spobject {

      public    $name;

      protected $app;         


      public function __construct($name) {

         code

      }


      public function __get($property) {

         code

      }


      public function __set($property, $value) {

         code

      }


      public function __destruct() {

         code

      }

    }

  

This class is the 'mother' of all objects. If you write code in PHsPeed, you most likely will work with the available objects and don't have to create new, but for the concept it is important to know that spobject is the root of the PHsPeed object model.


The declaration starts with 'class spobject'. Every class start with a class statement. In PHsPeed all classes are contained in a PHP file of the same name. This is very common in PHP environment that use autoloaders for PHP modules (for experience devs: you will not see many $includes in PHsPeed).


The we see two class variables, called 'properties'. These are variables that have an access type. There are three of them:


public: property can be access by any other object

protected: property can only be accessed by the class itself and objects that are descendants of this class (we get to that)\

private: property can only be accessed by the class itself and not outside.


Next we see a number of functions that belong to that class. They too can have an accessiblity declaration. These functions are called 'methods'. So __construct is a method of the class spobject.


Important!


  • To be able to use classes you need to create them. You do that with the statement new.


$myobject = new spobject('myname').


The function new requires that you define a initialization function called __construct(some parameters). __construct is always called when you create a new object. (If you define a class without a constructor then you have to define some initialization method to initialize the object. So use __construct).


  • If the object gets destroyed (i.e. at the end of your php program, php will clean up the trash) then the destructor function will be called. 

  • getters and setters


You see a __get and __set method. These are formal ways to move data around from objects to objects. PHP invokes these functions automatically and as all components of PHsPeed are descendants of spoject you don't have to worry about this. 


  • references


To access a property, you must point to that variable:


      public function __construct($name) {

         $this->name=$name;

      }


$this is required if you access a property of the current class.


Inheritance


The spobject is a very simple object, that has limited functionality, yet is is very important because it is the mother of all other PHsPeed objects. These other objects inherits the properties of the underlying objects. To understand lets look at the next sample:


Suppose we have a class 'graphic object'. There are several graphical objects, like a square, a triangle, etc. They all share some properties, like a position, color... But there are also differences. A square has four lines, a triangle three. So the structure could look like this (simplified):


class graphicalobject {

   public $x;

   public $y;

   public $color;

  

  public __construct($x, $y);

  public __destruct();

}


class triangle extends graphicalobject {

  $line1

  $line2

  $line3

}



class squaire extends graphicalobject {

  $line1

  $line2

  $line3

  $line4

}


In above sample the triangle and square inherits the properties and methods of the graphical object. You can see this as a 'kind-of' normalization of the object structure, similar to the normalization of a data structure of your database.


To improve we can define an object 'line' thathas properties, like, x, y coordinates for the startposition and end position of the line. And a method to draw that line:


class line {

  $x1; $y1;

  $x2; $y2;

  public __construct ($x1, $y1, $x2, $y2)  {  

     code

   } 

  

  public  function draw() {

    code        

  }

}


So now we have a class that can draw a line, and two classes that needs to draw lines:


class triangle extends graphicalobject {

  $line1

  $line2

  $line3

  public  __ construct (($point1, $point2, $point3) {

     $line1=new line($x1, $y1, ...

     $line2=new line($x1, $y1, ...

     $line3=new line($x1, $y1, ...

     parent::__construct(...;   

  }

  

 public function draw() {

   $line1->draw();

   $line2->draw();

   $line3->draw();

 } 

 

}


$point would need to be an object too, containing properties to hold the start and end position. As creating an object will call the constructor of that object, you need to take care that the constructor of the extended (=inherited from) objects are called. This is done by parent::__construct.(..); The :: sign means that you are working in the same object (as it is the same object you cannot use -> as it points to a different location). 


This example is not complete, but you now have an idea about how object orientation functions. If you do not understand fully then don't worry. Most of the time you will not need to use this.Important to know is that objects have properties that you accsss through object -> property.