Jump to content

Archived

This topic is now archived and is closed to further replies.

anevins

Geocoder Issue - Class Not Found

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?

Share this post


Link to post
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;
});

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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:

 

 

Share this post


Link to post
Share on other sites

×
×
  • 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.