free tutorials logo spacer
spacer
PHP
 
     
   
shaddow bottom left

222

All PHP code is in RED

Object Otriented Building Blocks

Surely you know the primitive data types, Strings, Integers, Floating point numbers (the ones with the decimal point),

Once we have a set of variables and functions (Methods and Properties) organized into a class, we can consider that class a new data type, one more complex than the primitive data types.

Now, you used to simply say $myname = "yazeed"; and the variable $myname would automatically be of type string, but our new class CAR can also be assigned to a variable like this

$anobject = new car();

Now $anobject is an Object of Type car, So despite the different name (Object), Object is in so many ways yet another variable, but rather than having a type like STRING it has the type CAR....

Roundup: You can think of a class as a dataype and an Object as a variable.

So, we know how to declare an Object (Variable), but where did the class car come from
Let me get to making a class, the we can explain it, the class will not be called CAR, we will call it vehicle since we want it to resemble cars, lories, trucks, and other kinds of mechanical vehicles.

class vehicle
{
public $price = "25000";
public $manufacturer = "Toyota"; public $engineSize = "2 Liter"; public $color = "White";
function getspecs() { return "Vhicle Manufacturer: {$this->manufacturer} \n<br> Engine Size: {$this->engineSize} \n<br> Color: {$this->color}\n<br> Price: {$this->price} "; }
}

Now let us instantiate an object from this class called $camry

$camry = new vehicle();

Now we have the class vhicle and the Object $camry

We can read the price of the camry like this

print $camry->price;

this should print 25000 to the browser, but the camry is for 31000 not 25000, So how can we fix this.

Right after Instantiating camry from Vehicle with the new operator, we can assigne to the price property of camry the new price like so.

$camry->price = 31000;

Now if we try to reprint (print $camry->price;) 31000 will display in our browser, You see, Just like any variable, we can read it or write to it, Just a different syntax for reading or writing to object variables.

Now what about the function, getspecs ? How can we access that.

print $camry->getspecs();

This will display

Vhicle Manufacturer: Toyota
Engine Size: 2 Liter
Color: White
Price: 31000

to our browser, So we got the basics, Classes encapsulate variables (Properties), and functions (Methods), we put them in a tidy little box.

Now, take a look, the $this in green up in the class definition is how things in the class are accessed from within the class, if we were to access price from within the class we say $this->price;, from outside the class we would say $camry->price (Object's name), in reality we deal with objects all the time (remember that Camry is an object, while vehicle is a class) Up to this point of writing, so we say $this inside the class to tell PHP that when we are in an object instantiated from vehicle, replace the word $this (behind the scene) with the Object's name (Thus making it identical to the external call $camry->price;), but we can not do it directly simply because Object names keep changing, and are sometimes dynamically generated.

NOTE: Do not take what i say literally, this is simply a plan to simplify your understanding of classes and objects while you get the hang of it. Soon you will see how easy it is.

So, is it really that simple, Yes, it is really that simple, but as you move forward you will find many easy things to do with classes and objects, in order to use them effectively, you will need some practice, but in general it is easy.

If you understand everything up to this point, Go make yourself a cup of maleeseh (A Jordanian substitute to Tea i had the privilege to try only once, and then i could find no more) or whatever beverage you would like to have, don't get 2 excited just yet, it gets much better.

NOTE: You would probably be thinking how can this serve me ? let me give you a quick example (Don't use it, it is simplified and skimmed), Let us say you have a database, and a CLASS with properties (Variables) with the same name as the database table columns ! ID, color, brand etc..., Then we make (In that class) 2 methods (Functions), one would be loadstate() and the other savestate(), You instantiate a new object, then you either fill in the data with your script or you go for loadstate($aRowId) and your object is loaded with data, once you finish working on it, You run the method savestate(); and your object saves itself to the database, Nice up to now don't you think ? but don't start coding anything serious just yet, i will give you more arms and ammunition in a few minutes.

NEXT: 04: Constructors, Extending the vehicle example 1


Your Name:

Subject:

Your Comment: