变量或函数(来自其他文件)对于该类的对象不可用,即使在包含该类的脚本中需要该文件

问题描述 投票:0回答:1

我有三个文件,即file1.php, file2.php and file3.php

file1.php中,我有一个变量var1

file1.php:

<?php

$var1 = 'hello world';
?>

我需要file1.php in file2.php并在file2.php中有一个课程

file2.php:

<?php 

require 'file1.php';

class demo{

function func1(){

echo $var1;
}

>?

file3.php中,我创建了一个演示类的对象,并调用了函数func1()。但是变量var1不可用。

file3.php

<?php
require 'file2.php';

$obj1 = new demo();
$obj1->func1();

所需的文件在类之外时是否不可用,但在同一文件中?是因为在调用函数file2.php中的require'file1.php'时未调用?为什么这样?有解决这个问题的好方法吗?

P.S。当我在file2.php的func1()函数中需要file1.php时,该变量可用

我有三个文件,分别是file1.php,file2.php和file3.php。在file1.php中,我有一个变量var1。 file1.php:我需要file2.php中的file1.php和...

php class require
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.