I have a switch in the index.php file. When there is a value in the $_GET['page'] and it matches the switch it includes a page, for example: "www.website.com/index.php?page=home". So basically you'll always be in the index.php file.

Above the switch I am calling the classes, but is there a better way? Should I call a new class within the pages? What is the best practice to call new classes?

$user = new \Project\classes\User();
$redirect = new \Project\classes\Redirect();
$validate = new \Project\classes\Validation();
$postData = new \Project\classes\Get();
$group = new \Project\classes\Group();
$session = new \Project\classes\Session();
$login = new \Project\classes\Login();
$unit = new \Project\classes\Unit();
$laboratory = new \Project\classes\Laboratory();
$ctgCode = new \Project\classes\Ctgcode();
$method = new \Project\classes\Method();
$material = new \Project\classes\Material();
$shipping = new \Project\classes\Shipping();
$labtest = new \Project\classes\Labtest();
$database = new \Project\classes\Database();

require_once HTML_HEADER;

if (array_key_exists('page', $_GET)) {
    switch ($_GET['page']) {
        case "home":
            require_once HTML_HOME;
        case "test_toevoegen":
            require_once HTML_FORM;
        case "login":
            require_once HTML_LOGIN;
        case "logout":
            require_once HTML_LOGOUT;
        case "register":
            require_once HTML_REGISTER;
            require_once HTML_HOME;

require_once HTML_FOOTER;
  • \$\begingroup\$ TL;TR: You instantiate classes when and where you need them. You do not create an instance of a class, if you're not going to need that class. So basically: create the classes in the files you're require-ing. PS: if $_GET['page'] is not set, you're only presenting the user with the header and footer, no content. Is that really what you want? \$\endgroup\$ – Elias Van Ootegem Oct 17 '14 at 15:19

The paradigm that I've seen is to put the class loading in a separate file that is required based on the page. Also note that it doesn't seem like you'd need every class on every page. Shipping leaps out at me as a class that is likely to be used on only some pages. Also Login (except the single question -- is the user logged in, which might live better in User).

I'm more accustomed to seeing the class files loaded right before they are used. In particular, the Database class is often loaded right before the first connection is made. See WordPress for an example in the require_wp_db() function.

Incidentally, this is an example of a question that might be better off on Stack Overflow. You aren't really asking us to review your code. It's more of a PHP question.


Captain obvious

IT seems as if all your classes are in the Project\classes namespace. Well, deuh! But why?, what is the benefit here? And why is Prject with a capital and classes without? Makes no sense.

What are namespaces

As from the php docs:

... namespaces are a way of encapsulating items.

You are encapsulating your classes in one big namespace. So no win there.

A more logical approach would be to have Services namespace with your ValidationService, Database, ... in it. A HTTPFoundation namespace with all your Request/Result classes (Session, Redirect, GET, POST, ...). You will probably have some DomainModels represneting the actual data (MAterial, LAbtest, ...)

The entire name of a class should explain exactly what it is. Project\classes\Unit tells is probably as bad as it can get.

Dependency injection

What you are doing is creating some global objects. You probably have some smelly method somewhere:

public function doSomething() {
    global $db;

And that is bad code! (just look up why dependency injection is better).

Once you use dependency injection. You will start using some Dependency injection container. A very simple and easy one I like it pimple (made by the symfony peeps).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.