How to import classes from their subclasses in PHP?

0

Basically, I have 3 files located in different directories:

diretorio_do_projeto/classes/config.php
diretorio_do_projeto/classes/abstract/abstract-dao.php
diretorio_do_projeto/classes/dao/modelo-dao.php

The config.php file defines constants for database access, and is included in the abstract-dao.php file, which in turn is included in the modelo-dao.php file.

The problem occurs when I include the modelo-dao.php file on some page that is not in a /classes subdirectory, the abstract-dao.php file is not found.

Is there a different way to import a class other than the require or include ?

The only solutions I could think of were to remove imports into classes and import them one by one on the page where I will use them, or leave them in the same directory as the page.

    
asked by anonymous 04.09.2014 / 15:57

4 answers

1

You can try doing this using namespaces.

It would be very similar to using Java packages and imports.

namespace Projeto\Dao;

class Pessoa {
}

It would be relative to the following in Java:

package projeto.dao;

public class Pessoa {
}

And to use the created classes:

use Projeto\Dao\Pessoa;
$pessoa = new Pessoa();

In Java:

import projeto.dao;
// ... public class, variaveis e etc.
Pessoa pessoa = new Pessoa();

The problem is that this does not work for magic and you need to configure PHP autoload to know where to look for the classes (know which folders to look for). I recommend dealing with autoload standards (PSR-0).

This article in Portuguese explains well the use of namespace: link

Edit

Tuyoshi Vinicius left a very important note: this is only available from version 5.3 of PHP.

    
04.09.2014 / 16:29
0

The only way to load files containing php classes is include , require , and so on. You can use the PSR-0 pattern that defines how files are loaded from an autoloader. You can find a good explanation here

A quick exit for your problem is loading the classes by path complete, for example:

require($_SERVER['DOCUMENT_ROOT'].'/diretorio_do_projeto/classes/abstract/abstract-dao.php');

But I still recommend using the PSR-0 standard, many frameworks like laravel already use this pattern.

NOTE: You can only use the PSR-0 standard from the PHP 5.3 version that supports namespace

    
04.09.2014 / 16:15
0

The best mechanism will be through an autoload mechanism of the type:

function autoload($class) {
    if (file_exists(LIBS_PATH . $class . ".php")) {
        require LIBS_PATH . $class . ".php";
    } else {
        exit ('A classe ' . $class . '.php em falta.');
    }
}
spl_autoload_register("autoload");

In this case LIBS_PATH is defined with "define" and should be the root of the directory where all the libraries to use will be included. As put in the question ... A set of sub directories is intended which can be included with an algorithm to search all sub directories before require.

In this way, it is enough to use the classes without performing "includes" or "requires" other than the autoloader itself.

spl_autoload_register uses a cache that is very useful in terms of performance in case of using a class more than once ... Among other features.

    
04.09.2014 / 16:30
0

To import a class without using require or include it is recommended that you use PSR-0 ( Link

a) a pattern that many have adhered to (even though it is discussed to use PSR-4 rather than PSR-0). It can be easily configured using the composer. A link to ease your setup: Link

There are other ways, like putting direct in the composer.json file, but since you do not use it, I think this is the easiest way.

    
04.09.2014 / 21:26