Oop 我的文件创建者类中存在问题?
我在我的文件创建者类中遇到了一个小问题。 我对OOP有点陌生,所以我认为我犯了一个错误 这是上课时间Oop 我的文件创建者类中存在问题?,oop,php,Oop,Php,我在我的文件创建者类中遇到了一个小问题。 我对OOP有点陌生,所以我认为我犯了一个错误 这是上课时间 <?php class Files{ public $filename ; public $content ; function Files($filename) { $this->filename = $filename; } function createfile() { $file = f
<?php
class Files{
public $filename ;
public $content ;
function Files($filename)
{
$this->filename = $filename;
}
function createfile()
{
$file = fopen($this->filename, "w+") or die('cant create file ');
return $file ;
}
function writetofile($content)
{
fwrite($this->createfile,$content) ;
}
function closecon()
{
fclose($this->createfile);
}
}
?>
您应该有一个名为$file的私有成员。 createfile不应返回文件句柄;相反,它应该将$this->file设置为有效的文件句柄。 writetofile应如下所示:
function writetofile($content)
{
$file != NULL || die ('didn\'t create file yet');
fwrite($this->file, $content);
}
class Files{
public $fp;
public $filename ;
public $content ;
function Files($filename)
{
$this->filename = $filename;
}
function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
function writetofile($content)
{
fwrite($this->fp, $content) ;
}
function closecon()
{
fclose($this->fp);
}
}
最后,closecon应该关闭$this->file指向的文件。享受吧 您应该有一个名为$file的私有成员。 createfile不应返回文件句柄;相反,它应该将$this->file设置为有效的文件句柄。 writetofile应如下所示:
function writetofile($content)
{
$file != NULL || die ('didn\'t create file yet');
fwrite($this->file, $content);
}
class Files{
public $fp;
public $filename ;
public $content ;
function Files($filename)
{
$this->filename = $filename;
}
function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
function writetofile($content)
{
fwrite($this->fp, $content) ;
}
function closecon()
{
fclose($this->fp);
}
}
最后,closecon应该关闭$this->file指向的文件。享受吧 您需要将文件指针资源存储在属性中,而不是每次调用createfile。此外,您甚至没有调用createfile,而是引用了一个不存在的属性,您应该得到一个通知。试着这样做:
function writetofile($content)
{
$file != NULL || die ('didn\'t create file yet');
fwrite($this->file, $content);
}
class Files{
public $fp;
public $filename ;
public $content ;
function Files($filename)
{
$this->filename = $filename;
}
function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
function writetofile($content)
{
fwrite($this->fp, $content) ;
}
function closecon()
{
fclose($this->fp);
}
}
另外,您的代码不是很适合PHP5。您的构造函数应该被称为_construct而不是文件,并且您的方法应该具有明确的可见性。我还建议您在实例化类时使用确切的事例:
class Files{
public $fp;
public $filename ;
public $content ;
public function __construct($filename)
{
$this->filename = $filename;
}
public function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
public function writetofile($content)
{
fwrite($this->fp, $content) ;
}
public function closecon()
{
fclose($this->fp);
}
}
$create = new Files('tmp/index.html');
您需要将文件指针资源存储在属性中,而不是每次调用createfile。此外,您甚至没有调用createfile,而是引用了一个不存在的属性,您应该得到一个通知。试着这样做:
function writetofile($content)
{
$file != NULL || die ('didn\'t create file yet');
fwrite($this->file, $content);
}
class Files{
public $fp;
public $filename ;
public $content ;
function Files($filename)
{
$this->filename = $filename;
}
function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
function writetofile($content)
{
fwrite($this->fp, $content) ;
}
function closecon()
{
fclose($this->fp);
}
}
另外,您的代码不是很适合PHP5。您的构造函数应该被称为_construct而不是文件,并且您的方法应该具有明确的可见性。我还建议您在实例化类时使用确切的事例:
class Files{
public $fp;
public $filename ;
public $content ;
public function __construct($filename)
{
$this->filename = $filename;
}
public function createfile()
{
$this->fp = fopen($this->filename, "w+") or die('cant create file ');
}
public function writetofile($content)
{
fwrite($this->fp, $content) ;
}
public function closecon()
{
fclose($this->fp);
}
}
$create = new Files('tmp/index.html');
首先,这是一个有效的代码片段:
class Files{
protected $filename ;
protected $content ;
protected $fd;
public function __construct($filename)
{
$this->filename = $filename;
}
public function createFile()
{
$this->fd = fopen($this->filename, "w+");
if ($this->fd === false) {
throw new Exception('Something bad happened');
}
}
public function writeToFile($content)
{
$length = strlen($content);
if ($length != fwrite($this->fd, $content)) {
throw new Exception('Something bad happened');
}
}
public function close()
{
fclose($this->fd);
}
}
$create = new Files('index.html');
$create->createFile() ;
$create->writeToFile('blah') ;
$create->close() ;
现在,变化如下:
您的构造函数应该称为_构造,而不是文件。
您缺少一个用于在此代码中存储文件句柄的属性,
命名fd
默认情况下,缺少这些方法的可见性,它们是
公共的,但我想实际指定它
方法的名称应该是小写的
创建文件
不要使用die,而是抛出异常
您没有检查写入错误
使财产私有或受保护
首先,这是一个有效的代码片段:
class Files{
protected $filename ;
protected $content ;
protected $fd;
public function __construct($filename)
{
$this->filename = $filename;
}
public function createFile()
{
$this->fd = fopen($this->filename, "w+");
if ($this->fd === false) {
throw new Exception('Something bad happened');
}
}
public function writeToFile($content)
{
$length = strlen($content);
if ($length != fwrite($this->fd, $content)) {
throw new Exception('Something bad happened');
}
}
public function close()
{
fclose($this->fd);
}
}
$create = new Files('index.html');
$create->createFile() ;
$create->writeToFile('blah') ;
$create->close() ;
现在,变化如下:
您的构造函数应该称为_构造,而不是文件。
您缺少一个用于在此代码中存储文件句柄的属性,
命名fd
默认情况下,缺少这些方法的可见性,它们是
公共的,但我想实际指定它
方法的名称应该是小写的
创建文件
不要使用die,而是抛出异常
您没有检查写入错误
使财产私有或受保护
这与问题无关,但您可能还希望使用_construct函数,而不是使用较旧的格式创建与类同名的方法。有关更多信息,请参阅。这与问题无关,但您可能还希望使用_构造函数,而不是使用较旧的格式创建与类同名的方法。有关更多信息,请参阅。方法的名称应为小写,如createFile Mmmm。。。为什么?这只是一个让名字更容易阅读的惯例。类名为大写,属性和方法为小写。有些语言强制使用,有些则不使用。方法的名称应该是小写的,比如createFile Mmmm。。。为什么?这只是一个让名字更容易阅读的惯例。类名为大写,属性和方法为小写。有些语言强制执行它,有些则不知道使用公共函数uu构造$filename和函数文件有什么不同$filename@Fouad:文件是PHP4语法。它仍在为向后兼容性而工作,但可能会被弃用,然后在未来的PHP版本中被删除可见性和类大小写也是如此。使用公共函数uu构造$filename和函数文件有什么不同$filename@Fouad:文件是PHP4语法。它仍在为向后兼容性而工作,但可能会被弃用,然后在PHP的未来版本中删除,可见性和类大小写也是如此。