How to import classes from your subclasses in PHP?

Asked

Viewed 2,802 times

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 archive config.php sets constants for database access, and is included in the file abstract-dao.php, which in turn is included in the file modelo-dao.php.

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

There is a different way to import a class other than via commands require or include?

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

  • You have already researched autoloads (http://php.net/manual/en/language.oop5.autoload.php) and namespaces (http://php.net/manual/en/language.namespaces.php)?

4 answers

1


You can try this using namespaces.

It would be very similar to using Java Packages and Imports.

namespace Projeto\Dao;

class Pessoa {
}

It would relate 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 by magic and you need to configure PHP’s autoload to know where to find the classes (know which folders to search in). I recommend the read on autoload standards (PSR-0).

This article in Portuguese explains well the use of namespace: http://www.diogomatheus.com.br/blog/php/entendendo-namespaces-no-php/

Edit

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

  • Thanks man, the analogy with Java helped a lot =)

0

The only way to load files that contain php classes is with include, require etc.. you can use the standard PSR-0 which defines how files will be loaded from an autoloader. You can find a good explanation here

a quick exit to your problem is to load the classes by path complete, example:

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

but still I recommend using the standard PSR-0, many frameworks like Larable already use this standard.

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

  • 1

    For new projects, it is much more interesting to adopt the PSR-4 instead of the PSR-0.

  • Another detail: PSR-0 has an adaptation that enables its implementation in earlier versions of php 5.3 (_ as directory separator). What is only supported from 5.3 is namespace. http://www.php-fig.org/psr/psr-0/

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 libraries to use will be included. As put in question... A set of subdirectories is intended that can be included with an algorithm to search all subdirectories before require.

This way just 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 cases of using a class more than once... Among other features.

0

To import a class without using require or include it is recommended to use PSR-0 (Link) a standard many adhered to (even though it was discussed to use the PSR-4 instead of the PSR-0). It can be easily configured using Composer. A link to facilitate your setup: Link

There are other ways, such as putting it directly in the Composer.json file, but since you don’t use it, I think this is the easiest way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.