What is the best way to include classes in php?

1

What is the best way to use classes in php?

That is, you can do a

include ("clase.php");

or I guess it will also work with a

require_once("clase.php");

I do not know if there are more ways or I know what is the best way to use it, on the php page it is not that it is explained too well.

    
asked by Pavlo B. 28.10.2016 в 15:39
source

4 answers

4

In fact, the official PHP documentation explains quite well the differences of using include("class.php"); to use require_once("class.php");

I summarize:

  

require_once

     

The require_once statement is identical to require except PHP   will verify if the file has already been included and if so, do not   include (require) again.

Now, require tells us:

  

require

     

The require statement is identical to include except that in   Failure will result in a fatal error of level E_COMPILE_ERROR . In   other words, this stops the script while it includes only   will issue a warning ( E_WARNING ) which allows you to continue   script.

Conclusion:

    
answered by 28.10.2016 / 15:53
source
1

They all import code from that PHP file, but what's the difference between using include () or require ()? Do I have to use include () or include_once ()?

require() vs include()

Both functions import or insert the code contained in the .php file into another. The difference can be deduced from its name:

require () establishes that the code of the invoked file is required, that is, mandatory for the operation of the program. Therefore, if the file specified in the require () function is not found, a "PHP Fatal error" error will be thrown and the PHP program will stop.

include (), on the other hand, if the code is not found, a "Warning" error will be thrown and the program will continue running (although as a consequence of not including the code it may not work correctly, or yes, it depends on the situation).

Therefore, it is more common to use require (), because the most normal thing is that if we call the code of another file it is because we need it. However, require is usually used to invoke code that, if it is not included, the program can reach very serious errors and therefore, in these circumstances, it is better to stop the execution of the program. And use include () to call files whose code does not affect other parts of the application and, therefore, if they are not, will not affect the rest of the program.

For example, if we have a PHP script to show articles it would be better to use require (), to load the code that makes the query to the database and receive the data of the article to be displayed, while we can use include () to invoke the file that contains html code such as the footer of the web page or another part of the template.

require_once()/include_once() vs require()/include()

The require_once () and include_once () versions work in the same way as their respective versions, except that, when using the _once version, the loading of the same file is prevented more than once.

If we include the same code more than once, we run the risk of redeclarations of variables, functions or classes. It is logical to think that because of this, it is always better to use the _once version. However, you should know that these versions are heavier and consume more resources and therefore must be used only when necessary.

    
answered by 28.10.2016 в 16:01
0

I think you confuse the subject, to include files You can do in several ways as they are:

  • include 'file.php'; // Include indefinitely in your file
  • include 'http: www.ruta.com/archivo.php';
  • include_once "file.php"; // Include once in your file
  • The idea of the includes is to do something generic.

    Another way is to make use of Require that is similar to include whose differences were discussed in the following link

    If you mean Classes, like the following:

    <?php
    class Cart {
    var $items;
    
    // Agregar $num artículos de $artnr al carrito
    
    function add_item($artnr, $num) {
        $this->items[$artnr] += $num;
    }
    
    // Sacar $num artículos de $artnr fuera del carrito
    
    function remove_item($artnr, $num) {
        if ($this->items[$artnr] > $num) {
            $this->items[$artnr] -= $num;
            return true;
        } elseif ($this->items[$artnr] == $num) {
            unset($this->items[$artnr]);
            return true;
        } else {
            return false;
        }
     }
    }
    ?>
    

    It can be included in an external file thanks to include and return a value

        
    answered by 28.10.2016 в 16:00
    0

    I believe that there is no right or wrong way, they all work, but good or bad practices.

    If you put the require, you are forcing the application to work with that library.

      

    require is identical to include except that in case of failure it will produce   a fatal error of level E_COMPILE_ERROR. In other words, this one   stop the script while include will only issue a warning   (E_WARNING) which allows to continue the script.

    require

    Include if you can not then issue a warning which does not stop the application but in the future may have some E_ERROR because of it.

    Everything depends.

    The cases with the eleven.

      

    The require_once statement is identical to r equire except PHP   will verify if the file has already been included and if so, do not   includes (require) again.

    require_once

    Simply put it in php.net, which if it is already added does not add it again, it is a way of not filling the code with another one already repeated, above it can sometimes give an E_ERROR commenting that the class in case of that are classes are already repeated.

    I hope I explained you well.

        
    answered by 28.10.2016 в 17:47