0

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

4
  • 2
    You need to learn about design patterns Commented Mar 11, 2015 at 17:48
  • Each class should be a tangible thing. You have a dataTable class and classes to work with the database. What you should have is a database class that has methods to produce a datatable and methods to update the database. In the end, your database class is not tied to a specific web page. It can be used in any web page. Commented Mar 11, 2015 at 17:51
  • Consider moving the project to a framework such as laravel.com , cakephp.org or symfony.com If nothing else you'll learn a lot about code organisation Commented Mar 11, 2015 at 17:51
  • Symfony has a long learning curve and is probably far more than you need or can handle right now. Codeigniter or Laravel would be better places to start with frameworks. Commented Mar 11, 2015 at 17:56

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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 (i.e. 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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.