PHP:如何在类外调用函数
我正在尝试将一个“记录器”实现到我正在使用的web工具中,它基本上是一个php文件,如下所示:PHP:如何在类外调用函数,php,logging,Php,Logging,我正在尝试将一个“记录器”实现到我正在使用的web工具中,它基本上是一个php文件,如下所示: <?php function makePost($post) { $file = fopen("log.txt", "a") or die ("Unable to open file"); $today = date("Y/m/d-h:i"); fwrite($file, $today .
<?php
function makePost($post) {
$file = fopen("log.txt", "a") or die ("Unable to open file");
$today = date("Y/m/d-h:i");
fwrite($file, $today . "\n====================================");
fwrite("\n" . $post);
fwrite("\n====================================");
fclose($file);
}
第8行和第9行的MakePost函数中有一个错误。通过注释第8行和第9行解决了这个问题
函数makePost($post){
$file=fopen(“log.txt”、“a”)或die(“无法打开文件”);
$today=日期(“Y/m/d-h:i”);
fwrite($file,$today.“\n===============================================================================”;
//fwrite(“\n”。$post);
//fwrite(“\n=============================================================”);
fclose($文件);
}
第8行和第9行的MakePost函数中有一个错误。通过注释第8行和第9行解决了这个问题
函数makePost($post){
$file=fopen(“log.txt”、“a”)或die(“无法打开文件”);
$today=日期(“Y/m/d-h:i”);
fwrite($file,$today.“\n===============================================================================”;
//fwrite(“\n”。$post);
//fwrite(“\n=============================================================”);
fclose($文件);
}
像这样更改它
<?php
function makePost($post) {
$today = date("Y/m/d-h:i");
$sep = str_repeat("=",20);
$res = file_put_contents('log.txt',"$today\n$sep\n$post\n$sep\n",FILE_APPEND);
if(!$res){
die ("Unable to open and write to file");
}
}
像这样更改它
<?php
function makePost($post) {
$today = date("Y/m/d-h:i");
$sep = str_repeat("=",20);
$res = file_put_contents('log.txt',"$today\n$sep\n$post\n$sep\n",FILE_APPEND);
if(!$res){
die ("Unable to open and write to file");
}
}
First do moverequire_once(“log.php”)代码>在类的外部和上方log.php
定义了一个全局函数,因此将其包含为全局函数。对于其余部分:您是否得到了任何可以在此处显示的错误?您必须首先初始化该类,然后调用该函数$a=urclass()$a->makePost();第二个尝试从类调用它的是该类与所有其他文件位于同一文件夹中。当前,log.txt
被wirtten到类文件不在log.php
所在的文件夹中。你有那个文件夹的权限吗?这是正确的文件夹吗?最后,这一行是您所需要的:file\u-put\u-contents('log.txt',“$today\n”.str\u-repeat(=”,20)。“\n$post\n.str\u-repeat(=”,20)。“\n”,file\u-APPEND)
创建数据或将数据附加到文件中。首先移动require\u-once('log.php”)代码>在类的外部和上方log.php
定义了一个全局函数,因此将其包含为全局函数。对于其余部分:您是否得到了任何可以在此处显示的错误?您必须首先初始化该类,然后调用该函数$a=urclass()$a->makePost();第二个尝试从类调用它的是该类与所有其他文件位于同一文件夹中。当前,log.txt
被wirtten到类文件不在log.php
所在的文件夹中。你有那个文件夹的权限吗?这是正确的文件夹吗?最后,您只需要这一行:file\u put\u contents('log.txt',“$today\n”.str\u repeat(=”,20)。“\n$post\n.str\u repeat(=”,20)。“\n”,file\u APPEND)
创建数据或将数据附加到文件中。
<?php
require_once("log.php");
class handleAccounts {
function loginValidation() {
makePost("A login was attempted");
// Some more code that isn't executed
}
}