如何在 PHP 中包含一个类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1994666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include a class in PHP
提问by CLiown
I have file index.php, and I want to include file class.twitter.phpinside it. How can I do this?
我有 file index.php,我想在其中包含 file class.twitter.php。我怎样才能做到这一点?
Hopefully, when I put the below code in index.php it will work.
希望当我将以下代码放入 index.php 时,它会起作用。
$t = new twitter();
$t->username = 'user';
$t->password = 'password';
$data = $t->publicTimeline();
回答by Mez
Your code should be something like
你的代码应该是这样的
require_once('class.twitter.php');
$t = new twitter;
$t->username = 'user';
$t->password = 'password';
$data = $t->publicTimeline();
回答by richsage
You can use either of the following:
您可以使用以下任一项:
include "class.twitter.php";
or
或者
require "class.twitter.php";
Using require(or require_onceif you want to ensure the class is only loaded once during execution) will cause a fatal error to be raised if the file doesn't exist, whereas includewill only raise a warning. See http://php.net/requireand http://php.net/includefor more details
如果文件不存在,使用require(或者require_once如果您想确保该类仅在执行期间加载一次)将导致引发致命错误,而include只会引发警告。有关更多详细信息,请参阅http://php.net/require和http://php.net/include
回答by Eric Leschinski
Include a class example with the usekeyword from Command Line Interface:
使用use命令行界面中的关键字包含一个类示例:
PHP Namespaces don't work on the commandline unless you also include or require the php file. When the php file is sitting in the webspace where it is interpreted by the php daemon then you don't need the require line. All you need is the 'use' line.
除非您还包含或需要 php 文件,否则 PHP 命名空间在命令行上不起作用。当 php 文件位于由 php 守护程序解释的网络空间中时,您就不需要 require 行。您所需要的只是“使用”行。
Create a new directory
/home/el/binMake a new file called
namespace_example.phpand put this code in there:<?php require '/home/el/bin/mylib.php'; use foobarwhatever\dingdong\penguinclass; $mypenguin = new penguinclass(); echo $mypenguin->msg(); ?>Make another file called
mylib.phpand put this code in there:<?php namespace foobarwhatever\dingdong; class penguinclass { public function msg() { return "It's a beautiful day chris, come out and play! " . "NO! *SLAM!* taka taka taka taka."; } } ?>Run it from commandline like this:
el@apollo:~/bin$ php namespace_example.phpWhich prints:
It's a beautiful day chris, come out and play! NO! *SLAM!* taka taka taka taka
创建一个新目录
/home/el/bin创建一个名为的新文件
namespace_example.php并将此代码放入其中:<?php require '/home/el/bin/mylib.php'; use foobarwhatever\dingdong\penguinclass; $mypenguin = new penguinclass(); echo $mypenguin->msg(); ?>调用另一个文件
mylib.php并将此代码放在那里:<?php namespace foobarwhatever\dingdong; class penguinclass { public function msg() { return "It's a beautiful day chris, come out and play! " . "NO! *SLAM!* taka taka taka taka."; } } ?>像这样从命令行运行它:
el@apollo:~/bin$ php namespace_example.php哪个打印:
It's a beautiful day chris, come out and play! NO! *SLAM!* taka taka taka taka
See notes on this in the comments here: http://php.net/manual/en/language.namespaces.importing.php
请参阅此处的评论中的注释:http: //php.net/manual/en/language.namespaces.importing.php
回答by AntonioCS
I suggest you also take a look at __autoload.
This will clean up the code of requires and includes.
我建议你也看看__autoload。
这将清理需要和包含的代码。
回答by peter p
require('/yourpath/yourphp.php');require_once('/yourpath/yourphp.php');include '/yourpath/yourphp.php';use \Yourapp\Yourname
require('/yourpath/yourphp.php');require_once('/yourpath/yourphp.php');include '/yourpath/yourphp.php';use \Yourapp\Yourname
Notes:
笔记:
Avoid using require_once because it is slow: Why is require_once so bad to use?
避免使用 require_once 因为它很慢:为什么 require_once 使用起来如此糟糕?

