0

I am writing a script to take care of my database migrations. I don't like using phinx, it doesn't allow for easy migrations for multiple databases.

My migrations are in directory

migrations/

I have read this question, and used it.

PHP script to loop through all of the files in a directory?

foreach (glob("migrations/*.php") as $filename) {
    require_once $filename;
    $t = explode(".",$filename);
    $obj = strtolower($t[1]);
    $class = explode("/", $t[0]);
    ${$obj}= new $class[1]();     // make object from the class name
    ${$obj}->up();
}

The file in migrations/ is Directory.php, it is a migration for the directory database, it has a method up() which I am trying to call. I want to run this script after provisioning, or manually via command line. It seems to be constructing the class ok. Getting the following error though...

PHP Fatal error: Call to undefined method Directory::up()

Community
  • 1
  • 1
Yoker
  • 500
  • 4
  • 20
  • you cannot construct objects from php files, you can create instance of class defined in php file – Iłya Bursov Dec 30 '14 at 00:36
  • Is that not the same thing? A want to create an instance of each class, so I can call the method up(), I thought the code I had would have worked? – Yoker Dec 30 '14 at 00:39
  • it is not the same, not all php files contains classes and one php file can contain several classes, if you know that your php file for sure has class Directory and this class has method up, try to remove `${` and `}` around $obj, just `$obj = new $class... $obj->up();` – Iłya Bursov Dec 30 '14 at 00:42
  • Didn't work, same error. – Yoker Dec 30 '14 at 00:55

0 Answers0