PHP Framework OOP model

2019-01-27 07:45发布

问题:

I think I am experienced procedural PHP programmer. I've implemented a few bigger projects. Now I would like to try OOP PHP on lesser one (framework for DB import/export + user authentication). Since I've never tried OOP on such a project I have problem with object design.

I would like to implement the framework the way, I'll just need to create one instance of the object to use whole framework. I would also like to keep the code well arranged, so I won't implement only one class for all methods/properties.

How could I split one big class in to few lessers to keep them organized (in more php files)?

I think I'll have to implement one base class and then extend it using the others. But that way I'll have many of small classes, not big one.

How would you solve this problem?

I hope I explained the problem well.

Thanks for any help.

回答1:

I have written and maintained huge monolithic libraries for many years, and I'm way happier since I moved away from it. Take my advice: Break your application down into as small classes as possibly makes sense. It's good for organizing, as well as memory usage (a huge PHP class definition can eat up a lot of RAM, and RAM is a limited resource in a PHP script).

Use PHP 5's autoloading mechanism to load only those classes thart you need in the current context.

I think the way Zend Framework is organized and built is quite good. You may want to take a look at how they designed their classes, and how they organized the immense amount of functionality in the framework.

As for how to organize tools and helper objects and libraries, I asked that question a few weeks back and got very good feedback. I'm still not done reading it, actually.



标签: php oop