Nested Class/Function and Code Completion [message #83149] |
Fri, 07 November 2008 20:32 |
Eclipse User |
|
|
|
Originally posted by: lafekafe.hotmail.com
Code Completion breaks when a class or function definition is nested
inside an if statement.
Case:
file1.php
if( !defined( 'FILE1' ) {
define( 'FILE1', true );
class Fish { public $dead; }
}
file2.php
require_once( 'file1.php' );
$fish = new Fish(); // Code Completion fails to find class Fish
$fish->dead = true; // Code Completion finds dead varibale in ...
// instance of class Fish
--
Same behaviour is found if you replace class Fish defintion with a
function.
file3.php
if( !defined( 'FILE3' ) {
define( 'FILE3', true );
function beerIsGood() { return true; }
}
|
|
|
|
|
Re: Nested Class/Function and Code Completion [message #83253 is a reply to message #83186] |
Sat, 08 November 2008 23:40 |
Eclipse User |
|
|
|
Originally posted by: lafekafe.hotmail.com
Andrew Thompson wrote:
> Why would you use constants here to check if the file is defined?
> Looks like you are trying to do something similar to C/C++ header
> file inclusion.
Simply because when you deal with a large project and lets just say...
under skilled developers when projects get created, it provides a
simple way to protect from nested includes, where developers 'forget'
to use require_once/include_once. This is done simlly for maintenance
complexity. It has since been reworked out of the code and all 'bad'
includes seded out of the projects and the nesting removed from library
code.
The point is, time required to fix all existing project's includes vs
protecting the include itself by nesting is pretty obvious.
> You can use require_once/include_once to include the file instead.
Indeed.
>
> I'd expect conditional declaration is bad practice anyway. If there
> were any need to do it, it could surely be done another way, e.g.
> subclassing Fish for different implementations.
Mentioning Subclassing here is kind of irrelivant as the code below is
a basic example demonstrating the behaviour of PDT.
The same conditional declaration would be used to define functions such
as if you were implementing, conditionally, memory_get_usage() in a
library designed to work with 'older' versions of php that do not have
it (<= 4.3.1, or <= 5.2.1 compiled without --enable-memory-limit).
So again, here the point is, there are perfectly valid reasons why you
would write conditional declarations.
>
> Andy
>
> Ben Clifton wrote:
> > Code Completion breaks when a class or function definition is nested
> > inside an if statement.
> >
> > Case:
> >
> > file1.php
> >
> > if( !defined( 'FILE1' ) {
> > define( 'FILE1', true );
> >
> > class Fish { public $dead; }
> >
> > }
> >
> >
> > file2.php
> >
> > require_once( 'file1.php' );
> >
> > $fish = new Fish(); // Code Completion fails to find class Fish
> > $fish->dead = true; // Code Completion finds dead varibale in ...
> > // instance of class Fish
> >
--
|
|
|
Re: Nested Class/Function and Code Completion [message #83265 is a reply to message #83214] |
Sat, 08 November 2008 23:43 |
Eclipse User |
|
|
|
Originally posted by: lafekafe.hotmail.com
Andrew Thompson wrote:
> Sorry I should have noticed your using require_once anyway. That
> should be enough to make sure file1.php is not included more than
> once, so you don't need the constant check.
>
No problem.
I should also point out this is a regression test issue also. Previous
versions of PDT handled this behaviour as expected.
> Andrew Thompson wrote:
> > Why would you use constants here to check if the file is defined?
> > Looks like you are trying to do something similar to C/C++ header
> > file inclusion.
> >
> > You can use require_once/include_once to include the file instead.
> >
> > I'd expect conditional declaration is bad practice anyway. If there
> > were any need to do it, it could surely be done another way, e.g.
> > subclassing Fish for different implementations.
> >
> >
> > Andy
> >
> > Ben Clifton wrote:
> >> Code Completion breaks when a class or function definition is
> nested >> inside an if statement.
> > >
> >> Case:
> > >
> >> file1.php
> > >
> >> if( !defined( 'FILE1' ) {
> >> define( 'FILE1', true );
> > >
> >> class Fish { public $dead; }
> > >
> >> }
> > >
> > >
> >> file2.php
> > >
> >> require_once( 'file1.php' );
> > >
> >> $fish = new Fish(); // Code Completion fails to find class Fish
> >> $fish->dead = true; // Code Completion finds dead varibale in ...
> >> // instance of class Fish
> > >
--
|
|
|
|
Re: Nested Class/Function and Code Completion [message #83330 is a reply to message #83307] |
Mon, 10 November 2008 21:40 |
Eclipse User |
|
|
|
Originally posted by: lafekafe.hotmail.com
Exception e wrote:
> Ben Clifton schreef:
>
> > The same conditional declaration would be used to define functions such
> > as if you were implementing, conditionally, memory_get_usage() in a
> > library designed to work with 'older' versions of php that do not have
> > it (<= 4.3.1, or <= 5.2.1 compiled without --enable-memory-limit).
> >
> > So again, here the point is, there are perfectly valid reasons why you
> > would write conditional declarations.
> >
> No, it's still ugly. For classes we have __autoload exactly for this issue. Stay away from includes and the like.
>
> If you want to reimplement memory_get_usage for example, I feet it is better to make a wrapper function and do a check there.
A perfectly valid approach, however it still leaves you in the position of locating and changing every occurance of memory_get_usage()
and changing it to memory_get_usage_wrapper() which is part of what I was originally trying to avoid.
Ben
--
|
|
|
|
Powered by
FUDForum. Page generated in 0.03134 seconds