Home Tutorial Php Phpoop PHP Create Instance

 
 

PHP Create Instance
Posted on: February 18, 2010 at 12:00 AM
Advertisement
In the current tutorial we will study how to instantiate an object in PHP. Example will help you to learn it precisely. You will also come to know about the use of new, -> operator.

Create an Instance:

In OOPs programming we need to instantiate each object. In the following example we create two classes A and B. To access variables of the class we should use self keyword following with double colon sign.

When we need to access the variables of a class from an object we use -> operator sign. To instantiate an object we need to use new operator as:

object-name=new class-name();

Following example is based on these concept, go through the example and run it on your computer, you will come to know how it works:

How to Create PHP Instance with Example:

<?php

class A

{

public static $one;

public function set($one){

self::$one=$one;

}

public function get(){

echo "Value of variable is:".self::$one;}

}

class B

{

public function disp(){

echo "<br/>Inside Class B";}

}

$a=new A();

$a->set(12);

$a->get();

$class='B';

$a=new $class();

$a->disp();

?>

 

Output:

Value of variable is:12
Inside Class B
Advertisement

Related Tags for PHP Create Instance:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 18, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com