Jump to content

Recommended Posts

Hello,

 

I'm using Geocoder (http://geocoder-php.org/) in my application.

I've installed it using the steps provided on the website.

 

Problem:

When I run the application, I receive the error;

Fatal error: Class 'Buzz\Browser' not found in D:\xampp\htdocs\dmp\geocoder\src\Geocoder\HttpAdapter\BuzzHttpAdapter.php on line 31

 

Line 31:

$this->browser = new Browser();

 

Entirety of BuzzHttpAdapter.php:

http://pastebin.com/GBDaWhWR

 

I've updated my PHP to the latest version.

 

I don't know how to solve this, would anyone be able to help?

Link to comment
https://forums.phpfreaks.com/topic/267889-geocoder-issue-class-not-found/
Share on other sites

That is due to php being unable to locate the class. Make sure you have something like this in your bootstrap file:

 

set_include_path(
    implode(PATH_SEPARATOR, array('.', realpath('path/to/library'))
);

spl_autoload_register(function($className)
{
    $className = ltrim($className, '\\');
    $fileName  = '';
    if ($lastNsPos = strripos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName;
});

In my autoload.php file, I have

spl_autoload_register(function($className) {
    $className = ltrim($className, '\\');
    if (0 != strpos($className, 'Geocoder')) {
        return false;
    }
    $fileName = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName = __DIR__ . DIRECTORY_SEPARATOR . $fileName . $className . '.php';
    if (is_file($fileName)) {
        require $fileName;

        return true;
    }

    return false;
});

 

I've tried adding

set_include_path(
    implode(PATH_SEPARATOR, array('.', realpath('path/to/library'))
);

But I don't know the library or directory I'm supposed to be pointing to, because I've tried looking for this class but not found it.

https://github.com/kriswallsmith/Buzz Your directory structure should look something like:

 

vendor
`- Buzz
`- Geocoder
autoloader.php

 

In your autoloader then should be:

 

set_include_path(
    implode(PATH_SEPARATOR, array('.', realpath('vendor'))
);

I'm now using this autoloader.php file

<?php

// autoload.php generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit::getLoader();

 

But the error persists.

I've tried adding

set_include_path(
    implode(PATH_SEPARATOR, array('.', realpath('vendor'))
);

But I do not follow the exampled directory structure you stated and I see no error change.

The following link shows my directory structure:

http://postimage.org/image/nvco6lljj/full/

 

I appreciate the help by the way.

If your class is named like this:

 

namespace Foo\Bar;

class Bat {}

 

And it is located in app/library/Foo/Bar/Bat.php. Then your include path should be:

 

set_include_path( realpath('app/library') );

 

So that when PHP looks for Foo\Bar\Bat it will find it in app/library/Foo/Bar/Bat.php. Change your directory structure or alter the include path so that it matches with where your classes live.

So my I want to set the include path to the directory of my file that is not found?

The browser.php file, from the original error Class 'Buzz\Browser' not found, has the namespace of Buzz.

 

This is the directory of the Browser.php file

D:\wamp\www\dmp\vendor\Buzz\lib\Buzz

 

When I

var_dump( realpath ( '../vendor/Buzz/lib/Buzz' ) ); 

I get

string 'D:\wamp\www\dmp\vendor\Buzz\lib\Buzz' (length=36)

 

And when I 

var_dump (set_include_path( ( realpath ( '../vendor/Buzz/lib/Buzz') )  ); 

I get

string '.;C:\php\pear' (length=13)

 

Here is my autoload.php file now

<?php

// autoload.php generated by Composer

require_once __DIR__ . '/composer' . '/autoload_real.php';

set_include_path( ( realpath ( '../vendor/Buzz/lib/Buzz') ) ); 

return ComposerAutoloaderInit::getLoader();

 

And this is the browser.php file

https://github.com/kriswallsmith/Buzz/blob/master/lib/Buzz/Browser.php

No, you'll want to set the path to where the file resides, minus the namespace part.

So if the file does indeed reside in the above location, then you'll want to set the include path to "../vendor/Buzz/lib". The rest is supplied by the namespace.

I now understand how to construct the correct realpath.

So here's my autoload.php file again

<?php

// autoload.php generated by Composer
//var_dump( realpath('../vendor/Buzz/lib'));exit;
set_include_path( ( realpath ( '../vendor/Buzz/lib') ) );

require_once __DIR__ . '/composer' . '/autoload_real.php';

return ComposerAutoloaderInit::getLoader();

 

I was thinking maybe I put this 'set_include_path' function using the wrong autoload file.

So I've tried putting it in all of them but it's the same issue.

 

This is the directory the autoload.php file is in

D:\wamp\www\dmp\vendor

 

This is the directory the other autoload files are in

D:\wamp\www\dmp\vendor\composer

 

This, again sorry, is the directory the Browser.php file is in

D:\wamp\www\dmp\vendor\Buzz\lib\Buzz

 

:shrug:

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.