1

I'm trying to use Podios PHP client library to create a new item in a Podio app.

I use the code from their example page but at the moment I try to:

// Create a field collection with some fields.
// Be sure to use the external_ids of your specific fields
$fields = new PodioItemFieldCollection(array(
  new PodioTextItemField(array("external_id" => "my-text-field", "values" => "FooBar")),
  new PodioProgressItemField(array("external_id" => "my-number-field", "values" => 75))
));

// Create the item object with fields
// Be sure to add an app or podio-php won't know where to create the item
$item = new PodioItem(array(
  'app' => new PodioApp(123), // Attach to app with app_id=123
  'fields' => $fields
));

// Save the new item
$item->save();

I get the following error:

PHP Fatal error: Cannot declare class PodioItemField, because the name is already in use in /var/www/html/podioproject/vendor/podio/podio-php/models/PodioItemField.php on line 0

I don't really understand what the error means so its hard for me to figure out what is loaded wrong and why.

There are other questions on stackoverflow where people answered to change include to include_once. I'm using composer's autoloader though, so that solution doesn't apply

composer.json:

{
    "require": {
        "podio/podio-php": "^4.3",
        "slim/slim": "^3.10"
    }
}
yivi
  • 42,438
  • 18
  • 116
  • 138
Maxi
  • 307
  • 1
  • 3
  • 13
  • @RonvanderHeijden But I'm only using composer autoloader and the rest is handled in their client library - so is the error in the client library? – Maxi Jun 15 '18 at 06:57
  • where is the PodioItemField class defined?, the class not in your question code – Sugumar Venkatesan Jun 15 '18 at 06:59
  • /vendor/podio/podio-php/models/PodioItemField.php. code is at https://github.com/podio/podio-php/blob/master/models/PodioItemField.php – Maxi Jun 15 '18 at 07:00
  • See this answer. https://stackoverflow.com/questions/38169097/fatal-error-cannot-declare-class – Jay Bhatt Jun 15 '18 at 07:10
  • 1
    It must be an issue with namespaces, have you checked using the namespace directly with no use statement? – lovelace Jun 15 '18 at 07:14
  • @lovelace I dont understand your comment? Where do I have a use statement? – Maxi Jun 15 '18 at 07:40
  • Did you install another version of Podios PHP before this one? You can use the following command to clear the cache: `php composer.phar clear-cache` or if composer is installed globally `composer clear-cache` – Franz Holzinger Jun 15 '18 at 07:56
  • @FranzHolzinger No I didn't. Clearing cache didn't help. – Maxi Jun 15 '18 at 08:29
  • 1
    I think that this problem comes from the fact, that the file `models/PodioItemField.php` includes many classes: `class PodioItemField extends PodioObject {`, `class PodioTextItemField extends PodioItemField { '` ... When composer needs a class, then it always loads this class file again and again. This happens for every class which is in this file. And the class `PodioItemField` has already been found before. Solution: Extract all those classes from the file `models/PodioItemField.php` into their own file, e.g. `models/PodioTextItemField.php` .... . – Franz Holzinger Jun 15 '18 at 12:42

1 Answers1

1

You are not doing anything directly wrong, the fault lies at the podio-php package.

Since it's declaring multiple classes in the same file, which clashes with how composer autoloader works.

If you take a peek at ClassLoader::includeFile($file) you'll find that it doesn't use an include_once call, but a plain include:

/**
 * Scope isolated include.
 *
 * Prevents access to $this/self from included files.
 */
function includeFile($file)
{
    include $file;
}

And if you take a look at the file where the error happens (PodioItemField.php), you'll see that in there you have the definitions for 19 different classes!

podioitemfield file structure

Maybe it would have been a better idea to use an include_once in composer's autoloader? Frankly, I don't know why it uses include instead, but I have to say that having multiple class definitions per file is not generally considered a good or recommended practice in any case. So generally speaking it wouldn't have made a difference.

Your remaining option to use the package is to load it outside composer. Just download the podio/podio-php package and include this in one of your files:

require_once '/path/to/podio-php/PodioAPI.php';

It sucks that the project is not correctly set-up, but it doesn't appear to have been updated very recently. Maybe you could fix it yourself and submit a pull-request! :)

yivi
  • 42,438
  • 18
  • 116
  • 138