S
S
Shimpanze2021-07-01 02:19:06
PHP
Shimpanze, 2021-07-01 02:19:06

Is it possible to automatically call a class in PHP?

Hello!

In procedural programming, it is possible to create a function and call it immediately (even with parameters). Something like:

( function( $a, $b ) {
  print $a + $b;
})( 2, 4 );


Can a class automatically call itself when I include a file with it via:

include_once 'my_class.php';

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FanatPHP, 2021-07-01
@Shimpanze

Firstly, this is not called procedural, but functional programming
Secondly, I again very much doubt that this is exactly what you need. In particular, "call a class" is nonsense, methods are called , not the classes themselves
Third, by analogy with functional programming, this is called an anonymous class

(new class {
    public function log($msg)
    {
        echo $msg;
    }
})->log("hello");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question