PHP 找不到类文件

发布于 2024-12-11 16:46:21 字数 831 浏览 0 评论 0原文

通常我会在创建对象之前包含我的类文件。我的问题是如何仅在主文件中包含类文件,然后在另一个类中使用它的方法?这是我的代码:

main file:

require_once("class.a.php");
require_once("class.b.php");

var $a;

function main () {
     $a = new class.a();
     $b = new class.b();
}

class b:

 class b {
    var $a;

    function __construct() {
          $this->a = class.a::method();
    }          
 }

这似乎在某些较旧的 PHP 版本中工作,但它给出了一个错误文件 class.a.php 在某些新的 PHP 版本中找不到。

编辑:我已经纠正了我的问题:class.b 使用直接调用 class.a 中的方法。这给了我一个错误 class.a.php not found 。当我像这样向 class.b.php 添加 require_once("class.a.php") 时,我可以修复此错误:

require_once("class.a.php");
class b {
    var $a;

    function _constructor() {
          $this->a = class.a::method();
    }          
 }

但是我有两个包含,而且这不适用于 php 的更新版本?

Normaly I would include my class file before I create an object. My question is how I can include a class file only in my main file and then use it method in another class? Here is my code:

main file:

require_once("class.a.php");
require_once("class.b.php");

var $a;

function main () {
     $a = new class.a();
     $b = new class.b();
}

class b:

 class b {
    var $a;

    function __construct() {
          $this->a = class.a::method();
    }          
 }

This seems to work in some older PHP version but it gives an error file class.a.php not found in some new PHP version.

Edit: I've corrected my question: class.b uses direct calls to method from class.a. This gives me an error class.a.php not found. I can fix this error when I add a require_once("class.a.php") to class.b.php like this:

require_once("class.a.php");
class b {
    var $a;

    function _constructor() {
          $this->a = class.a::method();
    }          
 }

But then I have two includes and also this doesn't work with an update version of php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千笙结 2024-12-18 16:46:21

好吧,new class.a() 应该会产生语法错误,因为 . 不是字符串连接之外的任何有效运算符。至于

class a {
    // do something
}

$a = new a();

require_once,好吧,一旦您在 PHP 脚本中包含或需要一个文件,所有的类/函数/变量/常量/等都会被调用。将继续保留在 require 之后包含的所有其他文件中。所以你这里的问题显然是语法问题。

此外,您可能需要考虑修改以下内容:

  • var $a; 已过时,它应该是 public $a (或 protected 或 private)。 var 仍然有效,因为它向后兼容 PHP 4,但这通常是一个坏主意。
  • 它是 __construct 而不是 _constructor
  • a::method(); 表示“调用类 a 的静态方法”。你想要类似
    $this->a = new a(); 的东西$this->a->method();

Well, new class.a() should create a syntax error as . is not a valid operator anywhere apart from string concatenation. You want

class a {
    // do something
}

$a = new a();

As to the require_once, well, once you have included or required a file in a PHP script, all of the classes/functions/variables/constants/etc. will continue to persist in all other files which are included after the require. So your problem here is clearly the syntax problem.

Additionally, you may want to consider modifying the following:

  • var $a; is outdated, it should be public $a (or protected or private). var is still valid because it is backwards-compatible with PHP 4, but it is generally a bad idea.
  • It is __construct not _constructor
  • a::method(); means "Call the static method of class a". You want something like
    $this->a = new a(); $this->a->method();
(り薆情海 2024-12-18 16:46:21

类名称中不应包含点 (.)。

您可能需要考虑使用自动加载器

You should NOT have dots (.) in class names.

And you might want to consider using an autoloader

请止步禁区 2024-12-18 16:46:21

按照其他答案中的说明更改课程名称。将它们设为“a”和“b”。您必须对 b 类使用依赖注入。

b {
    public $a;

    function __construct(a $a) {
        $this->a = $a; // now you have the instance of class.a in your class.b
   }          
}

还有你的 main.php

require_once("class.a.php");
require_once("class.a.php");

function main () {
     $a = new a()
     $b = new b($a);
     $b->a->method(); // call the method of a
}

Change the name of your classes as stated on other answers. Make them as "a" and "b". You have to use dependency injection on class b.

b {
    public $a;

    function __construct(a $a) {
        $this->a = $a; // now you have the instance of class.a in your class.b
   }          
}

And your main.php

require_once("class.a.php");
require_once("class.a.php");

function main () {
     $a = new a()
     $b = new b($a);
     $b->a->method(); // call the method of a
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文