开发者

problem within my file creator class?

im having small problem within my file creator class . im a little bit new to OOP so i think i made a fault here is the class

<?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);
    }
}
?>

and here is how i use it

<?php 
include 'classes/class.files.php';
$create = new files('tmp/index.html');
$content = '<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>';
$create->createfile() ;
$create->writetofile($content) ;
$create->closecon() ;
?>

when i call test.php file it give me this error

Warning: fwrite(): supplied argument is not a valid stream resource in C:\AppServ\www\cms\classes\class.files.php on line 16

Warning: fclose(): supplied argument is not a valid stream resource in C:\AppServ\www\cms\classes\class.files.php on line 20


You need to store the file pointer resource in a property (instead of calling createfile everytime). Also, you're not even calling createfile but referencing a non-existent property (for which you should get a notice). Try something like this:

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);
    }
}

Also, your code is not very PHP5-ready. Your constructor should be called __construct (not Files) and your methods should have explicit visibility. I also recommend you use the exact case when instanciating your classes:

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');


You should have a private member named $file. createfile should not return the file handle; rather, it should set $this->file to a valid file handle. writetofile should look like this:

function writetofile($content)
{
    $file != NULL || die ('didn\'t create file yet');
    fwrite($this->file, $content);
}

Lastly, closecon should close the file pointed to by $this->file. enjoy! :)


first, this is a working code snippet:

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() ;

now, the changes:

  • your constructor should be called __construct, not Files().
  • you missed a property for storing the file handle (in this code, named fd)
  • the visibility for the methods is missing (by default, they are public, but i like to actually specify it)
  • the names for your methods should be lower camel case (like createFile)
  • dont use die, but throw exceptions
  • you didnt check for fwrite errors
  • have the properties private or protected
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜