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
createfileeverytime). Also, you’re not even callingcreatefilebut referencing a non-existent property (for which you should get a notice). Try something like this:Also, your code is not very PHP5-ready. Your constructor should be called
__construct(notFiles) and your methods should have explicit visibility. I also recommend you use the exact case when instanciating your classes: