简体   繁体   中英

PHP multiple classes, Which inherit, interface, abstract?

I understand basic PHP classes and inheritance, but these don't solve in a elegant way what I want. I have been reading articles and watching video trying to get to grips with more advance OOP. But for the life of me I can not seam to find a neat solution for the following project. Any advice or hints?

I have a web application that is basically a fancy inventory and inspection record. One page has a sortable datatable, pop up dialog boxes and radio buttons to alter an items status. The page has classes for

baseClass = basic page construction, sets database connection and environment values
A dataTable class
A class that generates javascript and jquery code 
A class for managing status radio buttons
Classes and methods for updating the database

The problem is how to code these different class so they can be used on other pages and can be maintained/tweaked simply. At the moment the classes are called as and when needed using spl_autoload_register() but i get stuck ideas about Singleton, Dependancy Injection, Factories.

Any advice? (I am sure its a simple question, but banging my head against the wall is hindering me)

Thanks in advance

You can try Composer .

It is very easy and simple (all though, I didn't get a grip how to set up just a simple autoload, from their website). To set up simple autoload I used this tutorial: http://jessesnet.com/development-notes/2014/php-composer-autoloading/

Also read through PHPBridge it will give you good idea how to set up your classes and later use them with Composer.

Don't try to reuse classes, try to think in interfaces. Interfaces are behavioral contracts. They should be standard accross pages, implementations might vary.

For instance, you have an interface for getting the data. In practice might you use a mock class (ie hardcoded data in the class to test other parts of your application), you might get data from MySQL or from PostgreSQL or from any other source. All different implementations of the same interface. The interfaces should be able to communicate with each other, not the implementations themselves unless it is through their interfaces.

I would advice you to create this code yourself. It will give you the knowledge to understand frameworks better (in the long run).

I prefer the choice for implementation by configuration over auto-wiring, because it gives me more flexibility. In design patterns: I prefer a service factory over dependency injection, but both are good solutions.

Thanks everyone, As the time limit on this project is generous I'l spend a bit of time looking at design patterns again (I've only ever encountered structured patterns when editing existing projects) and look to doing the code myself as this is a secondhand project and starting from scratch is not really an option (yet!)

Once more thanks for your excellent advice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM