OBJECT


objects apg

Complex variable normally used in Object Oriented Programming.

The OBJECT, as a compound type, has common usage throughout PHP.





Of course, what we explain here, does not exhaust the subject: classes and objects; however, serves as the beginning of understanding.


We have a special chapter ...

... to study OBJECTS in more detail



 OBJECT AS IS 


To create an object, you must use the new statment to instantiate a class.



 class 


The fundamental class definitions is to use a keyword class followed by a name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.

The basic rules for establishing the name of a class in PHP are:

1 - Can not contain any reserved words;
2 - Can start with letters or underscore;
3 - Can not start by numbers;
4 - As a regular expression can be expressed thus:

^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$


A class may contain its own:

5 - constants, variables which are called properties,

and

6 - functions which are called methods.

One particular class provides the framework for data and actions, which are used in constructing objects.

In a same class can be constructed several objects, concomitantly, but independent of each other.

The class can be instantiated and saved in some variable using the keyword new, thus constituting an object.



<?php 

class test0x 


    
// class properties
    
    
public $v01 'Variable 1';
    
    . . . . . .
    
    public 
$v0n 'Variable N';

    
// and methods 
    
    
public function funcNAME()
    {
        
// function description
    
}
    



$obj01 = new test0x;

. . . . . .

$obj0N = new test0x;

?> 

  1 EXERCISE   

<?php 

class test01 

var 
$var01 'Alea jacta est'
var 
$var02 'Luck is on'

$obj01 = new test01

?> 

 class test01 


The name test01 followed by a pair of curly braces which enclose the definitions of two variables $var01 and $var02 as properties of the class test01.

The object is referenced by $obj01 = new test01 whose values of the built-in variables $var01 and $var02.



  2 EXERCISE   

<?php 

class test02 

public 
$var03 'Alea jacta est'
public 
$var04 'Luck is on'

$obj02 = new test02

$obj02->var03;
$obj02->var04;

?> 

 class test02 


The name test02 followed by a pair of curly braces which enclose the definitions of two variables $var3 and $var04 as properties of the class test02.

The object is referenced by $obj01 = new test02 whose values of the built-in variables $var03 and $var04 are presented by $obj02->var03 and $obj02->var04 respectively.