Do I need to include classes when making inheritance?

0

Next.

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

Imagine the class looks like this:

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

So, 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>

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
   }
?>

There, where I commented, in the second class, would not I need include / require? Put it here, testing, it does not make the mistake. But how does she find this class?

    
asked by anonymous 22.07.2017 / 03:56

1 answer

1

Let's assume that we have three files: two with class definitions and another that will be the code to execute.

Person.php

<?php

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

User.php

<?php

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

Note that the User class extends the Person class. If in the index.php file we 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 since all classes are defined it will work perfectly, even though we did not include the Person.php file in the User.php file since in the end everything was interpreted as a single file.

What the autoload function does is basically this: include all dependencies in the index.php file so that they are defined in all application files - based on the consideration that the application is always executed on the index.php file.

    
22.07.2017 / 04:33