1

I have a class setup like this:

class myClass {
    public function __construct() {
        echo 'foo bar';
    }
}

To run it, I can simply do:

$object = new myClass;

Is there any way to run the class so __construct initiates without creating a new variable with the class object.

For example, a basic function can be run with:

functionname();
Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • Clearly I'm learning OOP on a beginners level :) – Henrik Petterson Jul 26 '16 at 11:38
  • No. You need to create an instance of the class in order that the constructor runs. – Rizier123 Jul 26 '16 at 11:41
  • Raises the question.... why would you want to? The whole point of a constructor is that it's code that is run automatically when you do instantiate a class – Mark Baker Jul 26 '16 at 11:41
  • @Rizier123 I've seen something along the lines of `myClass::helloworld->...` so I thought there is a variation to just run the class so the constructor goes live. – Henrik Petterson Jul 26 '16 at 11:43
  • Of course, you can always do `new myClass()` without assigning it to anything, so the class will be instantiated, the constructor run, the the instance is immediately discarded.... but if your'e doing that, then you're simply misusing classes as though they were simply functions – Mark Baker Jul 26 '16 at 11:44
  • @HenrikPetterson I think you are getting confused with static methods (See: http://stackoverflow.com/q/4361598/3933332). Static methods don't rely on an instance of the class. So you can call them like: `myClass::myStaticMethod()` without creating an instance of the class. – Rizier123 Jul 26 '16 at 11:45
  • @MarkBaker Yes I know, `new myClass()` was what I was looking for. I just wanted to learn about if it is possible while I learn how to use it. – Henrik Petterson Jul 26 '16 at 11:45
  • 1
    If you see something like `myClass::helloworld`, then you're accessing a static property `helloworld` of the `myClass` class..... if that's followed by `->`, then that static property contains an instance of something – Mark Baker Jul 26 '16 at 11:45
  • Pretty unclear what your asking compared to the given code. Your example states your not calling a constructor when doing `new myclass`. You can just run `new myclass()` with constructor paranthesis without assigning it to a variable. If you want to call a function after construction, it's as simple as `(new myclass())->myfunction()` .. – dbf Jul 27 '16 at 18:11

3 Answers3

4

Don't call __construct directly. If you need something in the constructor to occur but you don't want an object created as a result, then use a static method.

class Thing{
    public static function talk(){echo "I talk";}
}
Thing::talk(); // 'I talk'

Static methods can be called without the need for an object instance of the class.

__construct is part of a special group of methods in PHP, called Magic Methods. You don't call these directly, but PHP will call them when some event occurs. For instance when you call new on a class, __construct is executed.

Another example: if you try to get a property that doesn't exist, __get will be executed (if found):

Class Thing{
    public property $name = 'Berry';
    public function __get($propertyName){
        return "$propertyName does not exist!";
    }
}
$t = new Thing();
echo $t->name; // 'Berry'
echo $t->weight; // 'weight does not exist!';
BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
1

You can try something like this if you want to avoid static:

(new MyClass)->myFunction();
PiotrCh
  • 103
  • 1
  • 5
0

I have no idea why you need this but you don't need to create a variable. You can just create object without store it anywhere.

class myClass {
    public function __construct() {
        echo 'foo bar';
    }
}

new myClass();

In this case only the constructor will be call.

zajonc
  • 1,935
  • 5
  • 20
  • 25