Monday, January 15, 2018

PHP Class Constructor

Classes of PHP

Let us first see a simple piece of code:-

 We  can learn my post OOP  (please Click this URL)  OOP ( object oriented Programming)  We just try to take that knowledge a bit further and add some juice into it.

We are imagining about a “Car class”. We want to make several new cars from it with different features. Next we’ll try to discuss about the advantages and limitations of using these new car objects.

Monetize your website traffic with yX Media

Let us see the code first: -

Class of PHP
Class of PHP code 

Run it either on your browser or on the terminal. Get this output:

string( 3) "RED" 

bool( false) 

It says the new car object “suzuki”is of red color. Then we ask, “Is it bought?”The default value is ‘false’so it comes out as “bool( false)”. Let us create another car object “maruti”and let it call the “buy()”method.

Look at the code:-

public function buy() { 

$ this-> is_bought = TRUE; 

The “buy()”method makes the property “is_bought”–TRUE :-

 $ maruti = new Car(" Blue"); 

var_dump( $ maruti); 

$ maruti-> buy(); 

var_dump( $ maruti-> is_bought); 

So when we create another car object “maruti”we get this output :-

object( Car)# 2 (2) {

 [" color"] = > 

string( 4) "Blue" 

[" is_bought"] = > 

bool( false) 

bool( true) 

You see the code now becomes more meaningful and close to the reality. It’s not that everybody goes to the car showroom to buy a car. For that reason we have kept the “is_bought”property “false”. When the consumer buys a car it sets to “TRUE”.

This scenario changes when two developers from two different parts of the world work on the same “Car class”. Suppose they both name their file “car.PHP”. A name collision is inevitable. What you can do to avoid such unpleasant situation? The answer is “Namespace"


Well, the name suggests it, as I have just said. It’s a great thing for developers as it is a great relief for them. They don't have to write unnecessary long name of methods to avoid name collisions.

As you know, 'foo.txt' can’t appear in the same directory. You have to place them in a separate directory like
            ‘/ home/ ss/ folderone’and
‘/ home/ ss/ foldetwo’and finally when you call ‘foo.txt’from a certain folder, you need to mention the whole path. Using the directory separator you can only reach ‘/ home/ ss/ folderone/ foo.txt’and the same rule is applicable for the other folders also.

   In PHP world, 'namespace' serves the same purpose. It, primarily, avoids name collision encapsulating items and applications can write the reusable codes without worrying about the name collisions. Now they can write short classes and functions.

            Before we go through an example there is one important thing to remember. In PHP 5 or higher version through namespace only classes (abstract classes and traits), interfaces, functions and constants are affected. READ MORE 


 <? php namespace 

MyProject\ sub\ level; 

const CONNECT_OK = 1; 

class Connection { ... }

 function connect() {....} 



 Declaring multiple namespaces in a single file is permissible but not desirable. In PHP 7, we’ll see later, how this problem has been solved.


Please comment and follow this site blog inbox