0

What i am trying to work out is it bad practice instantiate a class like

new Classname();

As i need to run the classes __construct but i do not need to use the class past that. as other functions within the class will be called from the __construct.

2
  • 1
    Why do you want to do this? It almost sounds like you're setting default values of a singleton.. Commented May 25, 2011 at 3:44
  • The only responsibility of the constructor is to prepare the object for use. It must not do anything beyond this and it must not have side effects. It is a bad practice, indeed, to put all the object's behaviour in the constructor. There are exceptions, of course, but if your class doesn't need a destructor then you didn't reach such an exception. Commented Oct 9, 2017 at 10:16

3 Answers 3

5

It'd work, but, as someone maintaining your code, I'd be super confused by that. Generally, merely instantiating an object has no side-effects, so I'd assume that I could just delete that line and everything would still work fine.

I'd recommend reconsidering your code's structure, since putting code with side-effects in __construct is definitely not standard.

Sign up to request clarification or add additional context in comments.

3 Comments

What the class does in clean input in the global scope, So it does not really need a variable.. So is it still worth just putting the variable there any way?
Chris, just for more understandability.
@Chris R, a better solution is something like $input = new InputFilter($_POST), and then use $input['foo'] instead of $_POST['foo']. If not using $_POST, you might have something like $globalfoo = new InputFilter($globalfoo). Using global variables inside functions (global keyword) is almost always a bad practice in of itself.
1

Consider using something like this instead:

class Foo
{
  public static function do_something()
  {
     // ...
  }
}

Foo::do_something();

While what you've got will work, it isn't clear that something is supposed to happen.

(If you insist upon using the object like that, at least document it clearly whenever you do that.)

1 Comment

This also possible and may be more, than with some variable. As seems, your code is more functional here, than OOP if you move your logic to __construct.
1

Well let just say that we have a classname called HelloWorld into a file.

File class.HelloWorld.php

class HelloWorld {

   function __construct()
   {
   }

   public function doSomething(){
       echo "new HelloWorld()->doSomething() was called";
   }

   public function anotherMethod(){
       echo "new HelloWorld()->anotherMethod() was called";
   }
}

Now you can instantiate the class on runtime without saving it into a variable.

require('class.HelloWorld.php');
// you can just instantiate it and the constructur will be called automatically 
(new HelloWorld());

// or you can instantiate it and call other methods
(new HelloWorld())->doSomething();

I'm not sure if the garbage collector will delete the instantiated classes or not, but i assume since those classes are not saved into a variable this would not be saved somewhere in the memory and that would be perfect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.