Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - What is the difference between “instantiated” and “initialised”?

I'm reading few books about PHP and getting stared to grab the basics. I came across “instantiated” and “initialised” words. I can not find an example which explains them.

What is the difference between “instantiated” and “initialised” in PHP ? What do they mean ? How to use them ? What's the purpose of using them ?

Provide an example if possible.

like image 554
Techie Avatar asked Jan 29 '13 16:01

Techie


2 Answers

You instantiate an object from a class. I.e. you create an instance (hence the name). In code:

$obj = new SomeClass();   

You initialise a variable, which means "giving it its initial (hence the name) value".

$var = "someValue";

In fact, when you instantiate, you also often initialise it (in the constructor). For example:

// this instantiates an object of class 'SomeClass' and 
// initialises it with "somevalue"
$obj = new SomeClass("someValue"); 

Instantiation is a object-oriented programming term. Initialisation is used in all languages. Both terms are certainly not limited to PHP.

like image 79
Bart Friederichs Avatar answered Sep 18 '22 23:09

Bart Friederichs


  1. Instances are where you have allocated memory for the variable but may or may not have placed a value in there.
  2. Initialized is where you have allocated memory as well as stored an intial value to it as well.

Just adding after reading Barts Answer that object oriented programming usually refers to instances in terms of objects being allocated memory while variables are said to be initialised which means allocated memory and assigned a value as well.

So for example

int $intarray=new Array(); // Instance created

while

int $intarray= new Array({1,2,3}); // instance created and initialised
like image 27
Farrukh Subhani Avatar answered Sep 21 '22 23:09

Farrukh Subhani