0
votes

im learning something about RedBeanPHP ORM and add the code downloaded from http://www.redbeanphp.com/downloadredbean.php to my project autoload using 'composer dump-autoload' command and the configuration 'composer.json' in the root directory is:

{
  "autoload": {
    "classmap": [
      "vendor/redbeanphp/src/rb.php",
      "vendor/myowncode/src/Model.php"
    ]
  }
}

on 'vendor/composer/installed.json' i put this:

[
    {
    "name": "gabordemooij/redbean",
    "version": "5.4",
    "require": {},
        "autoload": {
            "psr-4": {"RedBeanPHP\\": "src"}
        }
    },
    "name": "myowncode/src",
    "version": "1.0",
    "require": {},
        "autoload": {
            "psr-4": {"MyCode\\": "src"}
        }
    }
]

and all works fine, at least until i try the example from the RedBean web about 'Models' and the code:

<?php
require 'vendor/autoload.php';

class Model_Band extends RedBean_SimpleModel {
            public function update() {
                if ( count( $this->bean->ownMember ) >4 )
                throw new Exception( 'Too many members!' );
            }
    }

results in error:

PHP Fatal error:  Cannot declare class RedBeanPHP\RedException, because the name is already in use in /opt/lampp/htdocs/testing/vendor/redbeanphp/src/rb.php on line 8358

Fatal error: Cannot declare class RedBeanPHP\RedException, because the name is already in use in /opt/lampp/htdocs/testing/vendor/redbeanphp/src/rb.php on line 8358

but, if i dont use autoload and do this:

require 'vendor/redbean/src/rb.php';

class Model_Band extends RedBean_SimpleModel {
            public function update() {
                if ( count( $this->bean->ownMember ) >4 )
                throw new Exception( 'Too many members!' );
            }
    }

it works, but i want that works with the autoload, i know, i can just open composer.json file and add the package name ("gabordemooij/redbean": "dev-master"), but i want to learn more about autoload and get a good comprehension of whats wrong on my configuration/code.

1

1 Answers

0
votes

The problem was the code from http://www.redbeanphp.com/downloadredbean.php its not prepared for use with composer autoload, is some kind of amalgamation, all the code in a single file, and i try downloading a release from: https://github.com/gabordemooij/redbean/archive/v5.4.2.zip, i do the same process to generate autoload, but we must edit the file loader on RedBeanPHP dir on the release and change the REDBEANPHP_MAIN_DIR from phar://rb.phar/RedBeanPHP/ to vendor/redbean-5.4.2/RedBeanPHP/, i put the code on vendor/redbean-5.4.2, and thats all problem solved :)