|
|
Re: No PHP Compile Errors in eclipse PDT [message #66054 is a reply to message #66012] |
Wed, 13 February 2008 15:45 |
drm / Gerard Messages: 26 Registered: July 2009 |
Junior Member |
|
|
Mike Lubbers wrote:
> How can we get syntax- or compile- errors on non-existing methods
> instead of 'Fatal error: Call to undefined method...' during runtime?
Hi Mike,
I think there are two problems:
First, the interpreter of the code can not always exactly know what kind
of object you're talking to, except in those situations where the code
is obvious. This is due to PHP's weak typed nature.
Example:
Let's say you have a factory implementation for whatever. You know the
factory returns an object of a generic type, but you don't know the
exact instance type.
class Base { function b() {} }
class A extends Base { function x () {} }
class B extends Base { funciton y () {} }
/**
* @return Base an object
*/
function factory ( $whatever ) {
switch ( $whatever ) {
case 'a': return new A ();
case 'b': return new B ();
}
throw new Exception ( 'error!' );
}
$obj = factory ( 'a' );
Here we see the problem, the type of $obj can not be known by the
interpreter without actually running the file, since the type is defined
at runtime.
To make it even worse, we could also do:
try {
$obj = factory ( $_GET['someParam'] );
} catch ( Exception $e ) {
}
And there is definitely no way the interpreter knows what type $obj is.
So how should the IDE know if $obj has a method x() or a method y()? It
doesn't. It only knows we have documented factory() to return a Base
object, so that's all the IDE knows; the b() method however is known to
be there.
Second, there is always the possibility of a generic __call() method
which handles the unknown calls to an object. If an object implements
this method, that makes it even harder for the interpreter to figure out
if the method-call would result in an error or not.
class A {
function __call () {
}
}
$a = new A ();
$a->whateverWeWish ();
This doesn't generate an error at runtime, so how (moreover; why?)
should the IDE warn you about something that really isn't a problem?
To conclude: you can't look at PHP the way you can look at Java. In Java
everything is strong typed, even at compile time. In PHP it isn't, types
can change at runtime, at that's what makes it very hard to do
strong-type checks in the development environment (before runtime).
However, i think your problem in this case should actually be fixed by
refactoring possibilities in the IDE. Imho those could maybe be a bit
more extensive.
regards,
drm / Gerard
|
|
|
|
Powered by
FUDForum. Page generated in 0.03065 seconds