PHP - 无法从另一个文件访问某个类的公共静态方法


PHP - Cannot acces to public static method of some class from another file

我正在尝试访问php类中的公共静态方法,保存在与另一个文件不同的文件中,但是我收到错误

"网络错误: 500 内部服务器错误 - http://localhost/web/test.php"

型号.php

<?php
class Model
{
    public static function mensaje()
    {
        return "Mensaje";
    }
}

测试.php

<?php
$mensaje = Model::mensaje();
echo $mensaje;

如果我将 Model 类放在同一个测试文件中.php则可以完美地工作,但当我放入单独的文件时则不然。

模型.php和测试.php位于同一目录中。

如何从其他文件正确访问静态方法?

test.php中,您需要includerequire模型文件,以便它知道定义Model的位置:

<?php
require_once('Model.php');
$mensaje = Model::mensaje();
echo $mensaje;

http://php.net/manual/en/function.require-once.php

您只需要在当前使用的文件中包含或要求该文件

 <?php
 Require'test.php';
 //Or you can use  
 Include'test.php'; 
 // both will work 
 ?>