Hi All,

I am trying to create the instance of a class without using new.

class StaticVar
  {
  
   public $i;
    function __construct()
      {
       $i=8;
   
      }
    public  function StaticVar()
      {}
     public function display()
     {
       echo $i;
       echo $count;
     }
  } 
   $instance = new StaticVar();
   StaticVar $instance2;
    $instance->display();
     $instance2.display();

It is giving no error for $inatance , but is giving error for $instance2
Can anyone please explain why we cannot create an instance without new as we do in C++?

Hi All,

I am trying to create the instance of a class without using new.

class StaticVar
  {
  
   public $i;
    function __construct()
      {
       $i=8;
   
      }
    public  function StaticVar()
      {}
     public function display()
     {
       echo $i;
       echo $count;
     }
  } 
   $instance = new StaticVar();
   StaticVar $instance2;
    $instance->display();
     $instance2.display();

It is giving no error for $inatance , but is giving error for $instance2
Can anyone please explain why we cannot create an instance without new as we do in C++?

Hi,

Why are you trying to create an instance without using the new keyword?

If you need to call a method statically then its:

Classname::method();

example:

StaticVar::display();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.