[英]How do I use this PHP class in my code?
My directory structure: 我的目录结构:
bencode_test-
|--> BEncode.php
|--> bencode_test.php
|--> ubuntu-15.10-desktop-amd64.iso.torrent
my code:
<?php
require 'BEncode.php';
$bcoder = new BEncode();
$torrent = $bcoder->bdecode( File::get('ubuntu-15.10-desktop-amd64.iso.torrent'));
var_dump($torrent);
?>
I got BEncode.php from this Github account . 我从这个Github帐户获得了BEncode.php。
When I run my code, bencode_test.php from the command line, the error I get is: 当我从命令行运行代码bencode_test.php时,出现的错误是:
PHP Fatal error: Class 'BEncode' not found in /home/user/bencode_test/bencode_test.php on line 3
Can someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?
Calling a class should be like this 叫课应该是这样的
your folder looks like this 您的文件夹看起来像这样
bencode_test # calling function from here
|--> BEncode.php
|--> bencode_test.php
|--> ubuntu-15.10-desktop-amd64.iso.torrent
index.php # code
So inside BEncode.php 所以在BEncode.php里面
public function myName($value)
{
$name = "My Name is :".$value;
return $name
}
So inside index.php 所以在index.php里面
<?php
require './bencode_test/BEncode.php';
$bcoder = myName("Ab");
//$torrent = $bcoder->bdecode( File::get('ubuntu-15.10-desktop-amd64.iso.torrent'));
//var_dump($torrent);
?>
The file you linked on GitHub is in a namespace. 您在GitHub上链接的文件位于名称空间中。 You have to add an alias for the class in the beginning of the file:: 您必须在文件的开头添加该类的别名::
<?php
use Bhutanio\BEncode\BEncode;
?>
So in conclusion: 因此,结论是:
<?php
use Bhutanio\BEncode\BEncode;
require 'BEncode.php';
$bcoder = new BEncode();
$torrent = $bcoder->bdecode( File::get('ubuntu-15.10-desktop-amd64.iso.torrent'));
var_dump($torrent);
Or if you don't want to add an alias, use the fully qualified class name: 或者,如果您不想添加别名,请使用完全限定的类名:
<?php
require 'BEncode.php';
$bcoder = new Bhutanio\BEncode\BEncode();
$torrent = $bcoder->bdecode( File::get('ubuntu-15.10-desktop-amd64.iso.torrent'));
var_dump($torrent);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.