之前添加后斜线
我具有此功能,可以在我的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
nn
public function __construct()n
{n
$this->setData($" ."$var1". ", $" ."$var2". ", $" ."$var3". ");n
//parent::__construct();n
}
nn
public function setData($" ."$var1". ", $" ."$var2". ", $" ."$var3". ")n
{n
$this->" ."$var1". "= $" ."$var1;". "n
$this->" ."$var2". "= $" ."$var2;". "n
$this->" ."$var3". "= $" ."$var3;". "n
}
nn
public function printData()
{n
echo "SomeThing : " . $this->" ."var1". ". " " . $this->" ."var2". ". "n";n
echo "Other : " . $this->" ."var3". ". "n";n
}
nn
}
nn
?>");
// Close the file handler
fclose($file);
}
但是当我称之为时,我会得到此错误:可追踪的致命错误:类创建者的对象无法转换为create.php中的字符串。
第37行是:
$this->" ."$var1". "= $" ."$var1;". "n
我在其他文件中这样称呼这样:
$lol = new Creator();
$lol->CreateFile($input);
$lol->InsertData($input, $input1, $input2, $input3);
如何解决此问题?
您正在使用错误的引号对要生成的整个类。尝试将双引号"
更改为单引号'
。错误是因为当您使用"$this"
PHP试图调用$this->__toString()
时,在此上下文中,$this
仍然是外部代码中的变量。使用'$this'
(单引号)不过是简单的字符串。
另一个解决方案是在$this
-"$this"