안녕하세요,파일에 포함 된 파일의 컨텍스트
저는 어떻게 파일이 PHP에서 작동하는지 배우려고합니다. 오늘 해결할 수없는 문제가 있습니다.
파일과 동일한 디렉토리에서 : 이건 내 시나리오입니다
- config.php
- db.php
- functions.php
- form.php
config.php를
<?php
$config['json_file'] = 'test.json';
functions.php
<?php
function writeInFile()
{
echo $config['json_file']; // just for debugging porpuses
file_put_contents($config['json_file'], json_encode(time()));
}
model.php
<?php
class Model{
public function __construct();
function create()
{
writeInFile();
}
}
form.php
<?php
include('config.php');
include('functions.php');
include('model.php');
$model = new \Model();
$m->create();
나는 form.php
내가이 오류 얻을 실행하면 :이 var에 있기 때문에 일어나는 것을 알고
Warning: file_put_contents() [function.file-put-contents]: Filename cannot be empty in functions.php
을 $config['json_file']
은 functions.ph에서 writeInFile()
안에 null입니다. 피. 그러나 이론적으로 form.php의 시작 부분에 포함시키고 있기 때문에 효과가 있습니다. 아니면 내가 틀렸어?
필자는 writeInFile()에 매개 변수를 전달해야한다고 생각합니다. – verbumSapienti
나는 그렇게 생각하지 않습니다. 이것은 file_put_contents가 파일 이름을 쓰기 위해 필요하기 때문입니다. config var가이 함수에서 null로 도착하기 때문입니다. – manix
@manix - 매개 변수로 함수에'$ config'를 전달할 수 있습니다. 또는 전역 변수로 선언 할 수 있습니다. 그렇지 않으면 범위를 벗어났습니다. 그래서 null 값을 보았습니다 -'$ config'가 범위를 벗어났습니다. – andrewsi