NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
[resolved] Code completion when object name is a variable


Joined: 08 Feb 2007
Posts: 67
Reply with quote


I'll let the attached image do most of the talking. I receive the error box after typing
Code:
$thetest->
and then pressing CTRL-Space to force code completion.




Here's the code in case you want to copy-paste to duplicate the error:
Code:
<?php
class main
{
   var $mainVar;
   
   function mainFunc()
   {
   }
}

class theExtender extends main
{
}

function getObject($objName)
{
   $objToReturn = new $objName();
   return $objToReturn;
}

$thetest = getObject('theExtender');
$thetest->

?>


[edited for spelling]


Last edited by nothsa on Thu Mar 01, 2007 6:14 pm; edited 1 time in total
View user's profileFind all posts by nothsaSend private message


Joined: 08 Feb 2007
Posts: 67
Reply with quote
Here's an even simpler example:




Code:
<?php
class main
{
   var $mainVar;
   
   function mainFunc()
   {
   }
}

$objName = 'main';

$test = new $objName;
$test->
 
?>
View user's profileFind all posts by nothsaSend private message


Joined: 08 Feb 2007
Posts: 67
Reply with quote
Can anyone at Nusphere confirm that this is a PhpED bug, or if I'm just doing something wrong? If it's a bug, will there be a fix?
View user's profileFind all posts by nothsaSend private message
Veteran

Joined: 24 Jan 2006
Posts: 311
Reply with quote
Hi there- looked at it, sorry it took a bit. We can not always determine what would be assigned to the variable wheny you create it - this dynamic typing nature of PHP for you. But We di have FAQ on this subject, explaining how to deal with that:
http://forum.nusphere.com/tip-type-hints-t1563.html
I hope it helps. BTW- thank you very much for answering other people's questions, it is always very much appreciated.
View user's profileFind all posts by yfaktorSend private message


Joined: 08 Feb 2007
Posts: 67
Reply with quote
Thanks for the reply. No need to reply quickly with an answer. I just like to know that someone has seen it and will look into it at some point =)

Unfortunately it doesn't look like that link helps in my situation. I'm trying to use a string ("$objName") to define the name of the object that I was to use, but it looks like PhpED doesn't recognise that. I use this often in larger projects to define which object to load.

I am unaware of the challenges associated with developing a code completion system, but would it be an option to make my code above work, possibly by checking to see if "$aVariable" is a string, and if it is, using the string value to reference an object or possibly even a function, like PHP does when it parses the code? E.g.

Code:
class testValue
{
    var $test1;

    function function1() {}
}

function testValue($arg1,$arg2) {}


$aVariable = 'testValue';

$aVariable(     // At this point it would pop up a code hint, highlighting $arg1

$obj = new $aVariable;
$obj->     // At this point, code completion would pop up


I realise that this could possibly be incredibly difficult to do, but it would be a major boon to code completion. Please feel free to give me technical details if you want to, as I would love to hear about the challenges involved with this.
View user's profileFind all posts by nothsaSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
Quote:
Unfortunately it doesn't look like that link helps in my situation

I think there is a kind of misunderstanding.
What YFaktor pointed out to works pretty well:


You may notice a phpdoc comment to tell the IDE what type will have the variable below.
Typehints are explained there: http://forum.nusphere.com/tip-type-hints-t1563.html


Quote:
if "$aVariable" is a string, and if it is, using the string value to reference an object or possibly even a function, like PHP does when it parses the code

no, php recognizes this during run-time, not while parsing.

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 08 Feb 2007
Posts: 67
Reply with quote
dmitri wrote:
Quote:
Unfortunately it doesn't look like that link helps in my situation

I think there is a kind of misunderstanding.
What YFaktor pointed out to works pretty well:

<snip>[img]<snip>

You may notice a phpdoc comment to tell the IDE what type will have the variable below.
Typehints are explained there: http://forum.nusphere.com/tip-type-hints-t1563.html

I tried the phpdoc @var, but I obviously didn't do it correctly. Thanks for the clarification. It's working perfectly! Very Happy

dmitri wrote:
Quote:
if "$aVariable" is a string, and if it is, using the string value to reference an object or possibly even a function, like PHP does when it parses the code

no, php recognizes this during run-time, not while parsing.

Thanks for the clarification.
View user's profileFind all posts by nothsaSend private message


Joined: 08 Feb 2007
Posts: 67
Reply with quote
Oops. It appears that I spoke too soon. The point of using a string (in your example, $objName) to specify the class name is that we don't know what class name will be used. Consider the following:
Code:
if ($someCondition)
   $objName = 'main';
elseif ($anotherCondition)
   $objName = 'another_main';
else
   $objName = 'something_else';

$test = new $objName;
/** @var */
$test->

I don't know what to put for the phpdoc @var because I don't know which $objName will be used. Let me know if I'm missing something.
View user's profileFind all posts by nothsaSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
If the classes have a common ancestor (or common interface), I'd recommend you to put it in the @var.
If the have no common ancestor, I'd say that code is not well designed and nobody would tell what methods are available, including phped's code insight Smile

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 08 Feb 2007
Posts: 67
Reply with quote
dmitri wrote:
If the classes have a common ancestor (or common interface), I'd recommend you to put it in the @var.
If the have no common ancestor, I'd say that code is not well designed and nobody would tell what methods are available, including phped's code insight Smile

Ah, I was focussing too much on this example and not enough on my real-world code. I just needed to wrap my head around the whole process, and put my base inheritance class in the @var.

Thanks for the help, dmitri. This is going to save me a ton of time =)
View user's profileFind all posts by nothsaSend private message
[resolved] Code completion when object name is a variable
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic