Is __autoload () for parent classes of startup classes?

The main.phpadded auto-loading and creates a new object:

function __autoload($class) {
    require_once($class . '.php');
}
...
$t = new Triangle($side1, $side2, $side3);

In Triangle.php:

class Triangle extends Shape {...}

Shape.php - abstract class:

abstract class Shape {
    abstract protected function get_area();
    abstract protected function get_perimeter();
}

I see that the function __autoloadcalls Triangle.php, but does it call at the same time Shape.php?

+3
source share
3 answers

No (not at the same time), but yes (it will be loaded and everything will work).

When you call new Triangle, it will see that the Triangle is a class that is not yet loaded, so it calls __autoload(). Then there will be require_oncea Triangle.php file.

Triangle.php , , (Shape), .

, , , , .

+8

, . , ,

echo "loaded $class!\n";

__autoload?

+2

autoload is executed every time a class definition cannot be found.

In your case, it is first called for a triangle, then the parser encounters a link to Shape in Triangle.php and then automatically loads Shape.php

<?php
function __autoload($class) {
    print "autoloading $class\n";
    require_once($class . '.php');
}

$t = new Triangle();

[~]> php test.php 
autoloading Triangle
autoloading Shape
+1
source

Source: https://habr.com/ru/post/1717616/


All Articles