Book a Demo

Author Topic: PHP code generation bug?  (Read 4555 times)

Arjen van Haren

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
PHP code generation bug?
« on: July 29, 2008, 10:08:27 pm »
Hi,

Not sure if it's a bug or I'm doing something wrong, but I am generating PHP source code of classes in different subdirectories referencing each others. In the PHP files require_once (includes) are being made. However the generated file paths are relative.

But when doing includes in PHP, then the second include in the next file will use as a base path the first include that was used. In another words, you cannot use relative includes!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: PHP code generation bug?
« Reply #1 on: July 30, 2008, 08:03:00 am »
Interesting... It's the way it is because a lot of PHP developers requested it that way.

Arjen van Haren

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: PHP code generation bug?
« Reply #2 on: July 30, 2008, 05:24:42 pm »
Well, normally I would say also that relative includes are better to use then absolute ones. If your root location changes you don't have to change all includes.

The relative path is always depended on the current working directory, so for example:

index.php     dir1/file1.php (current working directory)
  |
   dir1/file1.php     include "file2.php"  NOT POSSIBLE when called from index.php

   dir1/file1.php     include "dir1/file2.php"  works, we begin from the basepath , called from index.php

   dir1/file2.php    

So a single relative include will work, but the second one will go wrong if not started from the current working dir of the first parsed php file.

This can work fine when your sure if the php files are always included the same way, but this is not very likely in a big project.

The thing we do in our projects:
- get the project root directory and place it in a variable
- make all includes absolutes by using the root variable:

include ($rootDir . "\dir1\file1.php");


I adjusted the php code generation template, used the absolute path and after that did a search and replace of the root directory with the $rootDir .


Cheers



«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: PHP code generation bug?
« Reply #3 on: July 30, 2008, 11:00:57 pm »
Just to clarify things a bit...

Did it work? And if so, how well?
No, you can't have it!

Arjen van Haren

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: PHP code generation bug?
« Reply #4 on: July 31, 2008, 01:07:34 am »
Yes, it worked for me. In the code template for PHP , section Import I changed the line:

$file = %importRelativeFilePath%

to

$file = %importFilePath%

The target directory for my generated code is: c:\codeg

Then I did a search and replace of the files with:
search on: 'c:\codeg
repplace:  $baseDir . '


I know there is probarly a nicer way to implement the basedir directly in the php codetemplate but EA is pretty new to me so I decided to go for the quick solution for now.