0

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++?

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by digital-ether
0

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();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.