Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » PHP Development Tools (PDT) » Nested Class/Function and Code Completion
Nested Class/Function and Code Completion [message #83149] Fri, 07 November 2008 20:32 Go to next message
Eclipse UserFriend
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 #83186 is a reply to message #83149] Sat, 08 November 2008 11:23 Go to previous messageGo to next message
Andrew Thompson is currently offline Andrew ThompsonFriend
Messages: 10
Registered: July 2009
Junior Member
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 #83214 is a reply to message #83186] Sat, 08 November 2008 11:26 Go to previous messageGo to next message
Andrew Thompson is currently offline Andrew ThompsonFriend
Messages: 10
Registered: July 2009
Junior Member
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.

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 #83253 is a reply to message #83186] Sat, 08 November 2008 23:40 Go to previous messageGo to next message
Eclipse UserFriend
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 Go to previous messageGo to next message
Eclipse UserFriend
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 #83307 is a reply to message #83253] Mon, 10 November 2008 11:50 Go to previous messageGo to next message
exceptione is currently offline exceptioneFriend
Messages: 96
Registered: July 2009
Member
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.
Re: Nested Class/Function and Code Completion [message #83330 is a reply to message #83307] Mon, 10 November 2008 21:40 Go to previous messageGo to next message
Eclipse UserFriend
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
--
Re: Nested Class/Function and Code Completion [message #83343 is a reply to message #83330] Mon, 10 November 2008 22:17 Go to previous message
exceptione is currently offline exceptioneFriend
Messages: 96
Registered: July 2009
Member
> 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.

Hm, if you want to avoid that, then a wrapper won't make sense indeed. I
agree that conditional definition shouldn't blow up auto-completion.
Previous Topic:Code completion and ctrl-click: looking in wrong directory
Next Topic:Template problems.
Goto Forum:
  


Current Time: Wed Feb 08 11:59:24 GMT 2023

Powered by FUDForum. Page generated in 0.02173 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top