如何使用另一个文件中的PHP类

时间:2022-10-08 21:05:59

Let's say I've got two files class.php and page.php

假设我有两个文件class.php和page.php

class.php

class.php

<?php 
class IUarts {
    function __construct() {
        $this->data = get_data('mydata');
    }
}
?>

That's a very rudamentary example, but let's say I want to use:

这是一个非常难以理解的例子,但是我想说我想用:

$vars = new IUarts(); 
print($vars->data);`

in my page.php file; how do I go about doing that? If I do include(LIB.'/class.php'); it yells at me and gives me Fatal error: Cannot redeclare class IUarts in /dir/class.php on line 4

在我的page.php文件中;我该怎么做呢?如果我包括(LIB。'/ class.php');它对我大吼大叫并给我致命错误:无法在第4行的/dir/class.php中重新声明类IUarts

4 个解决方案

#1


47  

You can use include/include_once or require/require_once

您可以使用include / include_once或require / require_once

require_once('class.php');

Alternatively, use autoloading by adding to page.php

或者,通过添加到page.php来使用自动加载

<?php 
function __autoload($class_name) {
  require_once $class_name . '.php';
}

$vars = new IUarts(); 
print($vars->data);    
?>

It also works adding that __autoload function in a lib that you include on every file like utils.php.

它还可以在libs中添加__autoload函数,该函数包含在utils.php等每个文件中。

There is also this post that has a nice and different approach.

这篇文章也有一个很好的和不同的方法。

Efficient PHP auto-loading and naming strategies

高效的PHP自动加载和命名策略

#2


8  

In this case, it appears that you've already included the file somewhere. But for class files, you should really "include" them using require_once to avoid that sort of thing; it won't include the file if it already has been. (And you should usually use require[_once], not include[_once], the difference being that require will cause a fatal error if the file doesn't exist, instead of just issuing a warning.)

在这种情况下,您似乎已经将文件包含在某处。但是对于类文件,你应该使用require_once“包含”它们以避免这种情况;如果文件已经存在,它将不包括该文件。 (并且你通常应该使用require [_once],不包括[_once],不同之处在于,如果文件不存在,则require会导致致命错误,而不仅仅是发出警告。)

#3


4  

Use include_once instead.
This error means that you have already included this file.

请改用include_once。此错误表示您已包含此文件。

include_once(LIB.'/class.php');

include_once(LIB '/ class.php');

#4


-1  

Use include("class.classname.php");

使用include(“class.classname.php”);

And class should use <?php //code ?> not <? //code ?>

并且类应该使用<?php // code?>而不是<? //代码?>

#1


47  

You can use include/include_once or require/require_once

您可以使用include / include_once或require / require_once

require_once('class.php');

Alternatively, use autoloading by adding to page.php

或者,通过添加到page.php来使用自动加载

<?php 
function __autoload($class_name) {
  require_once $class_name . '.php';
}

$vars = new IUarts(); 
print($vars->data);    
?>

It also works adding that __autoload function in a lib that you include on every file like utils.php.

它还可以在libs中添加__autoload函数,该函数包含在utils.php等每个文件中。

There is also this post that has a nice and different approach.

这篇文章也有一个很好的和不同的方法。

Efficient PHP auto-loading and naming strategies

高效的PHP自动加载和命名策略

#2


8  

In this case, it appears that you've already included the file somewhere. But for class files, you should really "include" them using require_once to avoid that sort of thing; it won't include the file if it already has been. (And you should usually use require[_once], not include[_once], the difference being that require will cause a fatal error if the file doesn't exist, instead of just issuing a warning.)

在这种情况下,您似乎已经将文件包含在某处。但是对于类文件,你应该使用require_once“包含”它们以避免这种情况;如果文件已经存在,它将不包括该文件。 (并且你通常应该使用require [_once],不包括[_once],不同之处在于,如果文件不存在,则require会导致致命错误,而不仅仅是发出警告。)

#3


4  

Use include_once instead.
This error means that you have already included this file.

请改用include_once。此错误表示您已包含此文件。

include_once(LIB.'/class.php');

include_once(LIB '/ class.php');

#4


-1  

Use include("class.classname.php");

使用include(“class.classname.php”);

And class should use <?php //code ?> not <? //code ?>

并且类应该使用<?php // code?>而不是<? //代码?>