关于php的include相对路径问题

建立a/a.php

如下

<?php
include "b.php";
class a{
    public function helloa()
    {
        echo "helloa";
    }
    public function hellob()
    {
        $b=new b();
        $b->hellob();
    }
}

?>

建立a/b.php

<?php
class b{

    public function hellob()
    {
        echo "hellob";
    }
}

?>

在根目录建立c.php

<?php

include "../a/a.php";


$a =new a();
$a->hellob();

?>

则发现可以执行,因为

php 默认相对路径都是以,被访问页面所在路径为准的。无论一个入口页面,里面包含多少文件,相对路径,都是以这个页面为准

所以在这里include "../a/a.php"; 

会把a.php中包含的b.php这个文件也替换成../a/b.php因而可以执行

有任何疑惑可以看https://www.cnblogs.com/changbin/p/5017365.html

猜你喜欢

转载自blog.csdn.net/a843538946/article/details/85322313
今日推荐