Andy F
Andy F

Reputation: 235

CakePHP - Cannot redeclare class conflicting with 3rd party library

I'm working with an application in CakePHP 2.5.4 and trying to integrate a third party library to work with a video service. The library is here: https://github.com/vzaar/vzaar-api-php

I've copied the above library to app/Vendor/vzaar/

In one of my applications controllers, app/Controller/VideosController.php, I'm including it using:

require_once APP . DS . 'Vendor' . DS . 'vzaar' . DS . 'Vzaar.php';

Whenever I run any of it's functions I get a PHP Fatal error:
Error: Cannot redeclare class User

The library has classes such as User.php and Video.php. My application also has classes with these names.

So is the solution to rename them in the library? Or have I imported/used this in the wrong way? Renaming things seems a very convoluted way around this, although I understand the issue is the same class name can't be used in multiple places.

I've searched on here and Google for a solution but haven't found anything. I would imagine this is a fairly common issue since it's likely any OOP-PHP classes from third parties might share the same names as class names in a Cake application?

Any help would be appreciated.

Upvotes: 0

Views: 221

Answers (1)

floriank
floriank

Reputation: 25698

So is the solution to rename them in the library?

Yes. I would fork the library on Github and create a namespaced version of it. I'm surprised they haven't used namespaces. If this was intentionally then they should have prefixed the classes with something like Vzaar_, which was a common practice before namespaces became common. Bottom line is the vendor made a not so smart decision, especially because "User" and "Video" a very common names. Add namespaces, do a pull request, maybe they'll accept it.

Upvotes: 1

Related Questions