如何在php类中设置文件路径
这是我的php项目目录结构如何在php类中设置文件路径,php,Php,这是我的php项目目录结构 My_Project ->cache ->config ->lib TestClass.php 我的TestClass.php包含方法cacheSet(),它在cache/目录中创建文件。所以我的问题是如何在该方法中设置路径为CACHE\u DIR class TestClass{ ..... public function cacheSet($filename, $data) { $filename = CACHE_D
My_Project
->cache
->config
->lib
TestClass.php
我的TestClass.php
包含方法cacheSet()
,它在cache/
目录中创建文件。所以我的问题是如何在该方法中设置路径为CACHE\u DIR
class TestClass{
.....
public function cacheSet($filename, $data)
{
$filename = CACHE_DIR . '/' . $filename. '.cache';
$file = fopen($filename, 'w');
fwrite($file, serialize($data));
fclose($file);
}
}
如果您正在使用一个类,您很可能会这样做:
public function cacheSet($filename, $data){
$this->filename = CACHE_DIR . '/' . $filename. '.cache';
$this->file = fopen($this->filename, 'w');
fwrite($this->file, serialize($data));
fclose($this->file);
}
我不是100%确定,但如果是在课堂上,那么很可能是这样。然后在类开始时,在执行类TestClass{之前,您需要执行类似$class=new TestClass()的操作;您可以使用:
class TestClass{
.....
const CACHE_DIR = "your dir";
public function cacheSet($filename, $data)
{
$filename = CACHE_DIR . '/' . $filename. '.cache';
$file = fopen($filename, 'w');
fwrite($file, serialize($data));
fclose($file);
}
}
所以你使用了,你的问题是什么?