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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With