Is there a way to program 100% object-oriented in

2019-02-27 18:29发布

I guess, it's possible to call functions in a separate class, I call this object-oriented programming in PHP.

But at the beginning, there is always an index.php or something, which calls or instantiates another class.

Is there a way at all to have a class which is self-calling in a way like java does with

public static void main(String[] args) ...

标签: php oop
2条回答
叼着烟拽天下
2楼-- · 2019-02-27 18:55

You need an entry point into your application somewhere. Java's entry point happens to be a predefined named location which is automatically called when an app starts. PHP's entry point is the first line of the first file that executes. As such, you need to get the ball rolling with a single "procedural" call somewhere, which could simply be something like:

require_once 'controller.php';
new Controller;

That's essentially the same thing Java does, just more explicitly.

查看更多
混吃等死
3楼-- · 2019-02-27 19:12

Not by default. But you could write your own bootstrap that does this, see a related answer that outlines how you can do it.

If index.php is your only central entry point, it's already the bootstrap.

From that point on, you can do everything OOP you like. However, I think your question is less about OOP but more about how you deal with the invokation of your application code.

查看更多
登录 后发表回答