Thursday, 6 May 2021

Php: How to get an object from class?

You can get an object using new keyword followed by class name.

 

Syntax

$obj1 = new ClassName;

$obj2 = new ClassName(arguments);

 

Let’s try 1st syntax, I will explain second syntax in upcoming posts.

 

new_instance_demo.php

#!/usr/bin/php

<?php

   class Employee{

   }

   $emp1 = new Employee;

   echo '$emp1 is an instance of ' . get_class($emp1);
?>

 

Output

$./new_instance_demo.php 

$emp1 is an instance of Employee

 


 

Previous                                                    Next                                                    Home

No comments:

Post a Comment