1

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.

When I run my code, bencode_test.php from the command line, the error I get is:

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?

user465001
  • 806
  • 13
  • 29

2 Answers2

0

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

public function myName($value)
{
    $name = "My Name is :".$value;
    return $name
}

So inside 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);
?>
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

The file you linked on GitHub is in a namespace. 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);
?>
SOFe
  • 7,867
  • 4
  • 33
  • 61