i have this function that insert code in my file.php:
function InsertData($nombre, $var1, $var2, $var3)
{
$file = fopen("$nombre".".php", "r+");
// Seek to the end
fseek($file, SEEK_END, 0);
// Get and save that position
$filesize = ftell($file);
// Seek to half the length of the file
fseek($file, SEEK_SET, $filesize / 1);
// Write your data
fwrite($file, "<?php
class " ."$nombre". "\n
{\n
private $" ."$var1;". "\n
private $" ."$var2;". "\n
private $" ."$var3;". "\n
\n\n
public function __construct()\n
{\n
$this->setData($" ."$var1". ", $" ."$var2". ", $" ."$var3". ");\n
//parent::__construct();\n
}
\n\n
public function setData($" ."$var1". ", $" ."$var2". ", $" ."$var3". ")\n
{\n
$this->" ."$var1". "= $" ."$var1;". "\n
$this->" ."$var2". "= $" ."$var2;". "\n
$this->" ."$var3". "= $" ."$var3;". "\n
}
\n\n
public function printData()
{\n
echo \"SomeThing : \" . $this->" ."var1". ". \" \" . $this->" ."var2". ". \"\n\";\n
echo \"Other : \" . $this->" ."var3". ". \"\n\";\n
}
\n\n
}
\n\n
?>");
// Close the file handler
fclose($file);
}
but when i call it i get this error: Catchable fatal error: Object of class Creator could not be converted to string in create.php on line 37
line 37 is:
$this->" ."$var1". "= $" ."$var1;". "\n
im calling it like this in other file:
$lol = new Creator();
$lol->CreateFile($input);
$lol->InsertData($input, $input1, $input2, $input3);
How can i solve this?
You are using wrong quotes for the whole class you are trying to generate. Try to change double quotes
"to single quotes'. The error is because when you use"$this"PHP is trying to call$this->__toString(), in this context is$thisstill a variable from your outside code. Using'$this'(in single quotes) is just nothing more than simple string.Another solution whould be to add backslash before
$this–"\$this"