NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Code Insight and Regsitry


Joined: 15 Sep 2005
Posts: 51
Location: Birmingham, UK
Reply with quote
Hi

Not really a PhpEd question but I'm wondering if anyone has a work around for the following.

I use a regsitry to store my dbconn object and a few others, if I want my db object I get it from my regsitry with the below.

Code:
$this->dbconn = $registry->get('dbconn');


The problem with this, is that code-insight doesn't recognise the type of object being returned and therefore I don't get any of the hints for the objects I store.

Anyone got any ideas on a possible workaround.
View user's profileFind all posts by BlackGasSend private messageVisit poster's website


Joined: 14 Jun 2007
Posts: 67
Reply with quote
when defining $dbconn as a property of the object you're using, use PHPDoc comments to set the type.

eg.
/**
* @var $dbconn DBConn
*/
View user's profileFind all posts by CjungeSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
if $registry->get() returns instance(s) of the same class, it's more appropriate to have @returns in the PHPDOC for this method, otherwise, you'll need to add typehints with all calls like suggested by Cjunge

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


Joined: 15 Sep 2005
Posts: 51
Location: Birmingham, UK
Reply with quote
Cheers Cjunge, can't belive I didn't think of that.

Dmitri, not sure setting return will work because dependant on the string the registry returns at the moment three different objects.
View user's profileFind all posts by BlackGasSend private messageVisit poster's website


Joined: 11 Sep 2005
Posts: 45
Reply with quote
Right...

Adding type hints for every instance of an object is tedious.

Dmitri your solution doesnt work because ->Get() doesnt always return the same object, it depends on the string passed in.
View user's profileFind all posts by bendjSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
Quote:
Dmitri your solution doesnt work because ->Get() doesnt always return the same object, it depends on the string passed in


Code Insight does never execute the code to see the results. Unfortunately you develop a code that can't be understood without executing (this is what people refer to as 'dynamic nature of php') therefore code insight can't recognize the classes without typehints.


There are two possible ways:

a) derive all possible returning classes from just one ancestor and move as much methods and properties to it (use "abstract" when appropriate).
then define your Get in the typehint as returning this ancestor class

b) if a) is not possible, you can apply typehints to the variables that you assign the value returned by Get()

Quote:
Adding type hints for every instance of an object is tedious.


That's why I recommended to apply typehints to properties and methods.

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


Joined: 11 Sep 2005
Posts: 45
Reply with quote
Quote:
That's why I recommended to apply typehints to properties and methods.
Quote:


Please provide an example of what you mention above.

This is still not working.
View user's profileFind all posts by bendjSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
Quote:
This is still not working.

do you mean your code or typehints?
If you need examples on how to use typehints please check FAQ forum.
If you have problems with typehints in your particular code fragment, please provide clear and short example.

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


Joined: 11 Sep 2005
Posts: 45
Reply with quote
I did:
/**
* @var $db NewADOConnection
*/
$db = $registry->get('DB');

Then later in the code if I start typing $db->
it does not bring up the code insight tooltip.
View user's profileFind all posts by bendjSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8334
Reply with quote
typehint shown below would be more correct:
Code:
/**
* @var NewADOConnection
*/


Please note that @var always relates to the variable assigned below so it does not require the variable itself. Although you can add it, but after the class name.

Regarding Code Insight Tooltip, it's not expected for the db->| position. You may get Code Insight popup with members of NewADOConnection class listed there. While Tooltip will be shown when you start entering method (or function) arguments.

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


Joined: 11 Sep 2005
Posts: 45
Reply with quote
Still can't get it to work.... using

/**
* @var Session
*/
$session = $registry->get('Session');

This doesnt give any code insight whatsoever.
View user's profileFind all posts by bendjSend private message


Joined: 11 Sep 2005
Posts: 45
Reply with quote
Does it matter if we do not specifically include the session class file? This is also automatically included by the registry using a factory design pattern (another class).

I figured phped would auto load any files within the project.
View user's profileFind all posts by bendjSend private message
Code Insight and Regsitry
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 2  

  
  
 Reply to topic