Do I need to include classes when I inherit?

Asked

Viewed 282 times

0

Following.

When I want to use something, for example, on the index, I want to pull a class to make an object, right?

Imagine the class is like this:

<?php
class Testando{
   //Meu codigo aqui
}
?>

Ai, I have my index:

<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
             require'Testando.class.phg';
             $Teste = new Testando;
        ?>
    </body>
</html>

Alas now, I have my second class, let’s suppose.. :

<?php
   //Aqui eu não precisaria de um require/include da class Testando?
   class EuTestei extends Testando {
       //Meu codigo aqui
   }
?>

Ali, where I commented, in the second class, would not need to include/require? Put here, testing, it not from error. But how does she find this class?

  • Are you using namespace? if you are using autoload it is not necessary to give include/require..

  • autoload I use, but do not understand, how that class extends to another, and I have not used require.

  • 1

    If this second file was also included in index.php, PHP finds the class because it was loaded in index.php through the first require. When you do the require or include, for PHP is as if all the codes were written in the same file, so the class Testando would be implemented before class EuTestei, so it works. If index.php you give require only the second file, so yes, you will need to give the require from the first where you commented.

  • I recommend you to study and understand how it works and what the autoload is for, because it already makes the complete load of all classes, and where you give a new in the object there already have all the classes of the system loaded.

  • I get it, Anderson, if you want to put the answer, then I mark it as you accept!

  • Thank you too Drik

Show 1 more comment

1 answer

1


Let’s assume that we have three files: two with the definitions of the classes and another that will be the code to be executed.

Person.php

<?php

class Person
{
    public $firstname;
    public $lastname;
}

User.php

<?php

class User extends Person
{
    public $username;
    public $password;
}

Notice that the class User extends the class Person. If in the file index php. do something like:

<?php

require "Person.php";
require "User.php";

$user = new User();
$user->firstname = "Anderson";
$user->lastname = "Woss";

For PHP, the executed file would be basically:

<?php

// require "Person.php";
class Person
{
    public $firstname;
    public $lastname;
}


// require "User.php";
class User extends Person
{
    public $username;
    public $password;
}

$user = new User();
$user->firstname = "Anderson";
$user->lastname = "Woss";

And how all classes are set will work perfectly, even if in the file User.php we have not included the file Person.php, because in the end, everything was interpreted as a single file.

What the function of autoload does is basically this: include all dependencies in the file index php., so they will be defined in all the files of the application - considering that the application is always executed on the file index php..

Browser other questions tagged

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