0

I'm using NetBeans since its version 7.0 and its my favorite IDE for PHP since than, but today I was using it for a project and its giving me problem in PDO code completion. There are two cases,

1) If I am instantiating the PDO object on the same page, than all of the code completion is correct.

2) But when I am keeping my PDO object in another file and requiring that file, than the code completion doesn't work.

EXAMPLES

1)

<?php
/**
 * In this case the code completion works fine
 */
try {
    $db = new PDO('mysql:host=localhost;dbname=tshop', 'root', 'mypass');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

2

<?php
/**
 * But in this case it doesn't work
 */
try {
    require_once '../../includes/database_connection.php';
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

Thank you Everyone in advance !

Bangash
  • 1,152
  • 3
  • 10
  • 30

1 Answers1

1

Adding a comment should fix the issue i beleive:

try {
    require_once '../../includes/database_connection.php';
    /* @var $db PDO */
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = 'SOME SQL QUERY';
    $result = $db->query($sql);
} catch (Exception $ex) {
    $error = $ex->getMessage();
}

That said if i were I would not go about establishing DB connections in the way because if you arent careful you can easily have multiple connections going by accident which could potentially exceed the max connections.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
  • @peodigitalson , Brother its not working. My NetBeans version is 8.0.2. – Bangash Feb 05 '15 at 21:28
  • Did you recently add the file you are requiring? Its also possible that the scanner jsut needs to run again – prodigitalson Feb 05 '15 at 21:29
  • 1
    Actually right from the start i am requiring that database_connection.php, the Example-1 was just for the test. I was just testing if I instantiate the PDO object on the same page it will work or not. i was just checking it. – Bangash Feb 05 '15 at 21:31
  • Yeah i ust double checked my NB isnt respecting the `@var` comment either. Could swore this worked in NB at one time because i didnt notice the feature missing when switching from Eclipse. – prodigitalson Feb 05 '15 at 21:38
  • AHHH syntax is wrong the label needs to come before the type, ie. `/* @var $db PDO */`. Updating my answer. – prodigitalson Feb 05 '15 at 21:41
  • Yes, it was working, but I think NetBeans team isn't serious anymore, because the mysqli object was also having code completion problems in 8.0.1, i haven't checked mysqli on this version, don't know if its fixed or not, I reported to netbeans' official website as well as to their twitter that time. – Bangash Feb 05 '15 at 21:41
  • You mean it was previously working just from the included file? If so pay attention when you open netbeans... maybe something is going wrong during the background scanning when you open the project. – prodigitalson Feb 05 '15 at 21:43
  • I have tested it several times, mysqli was working 3 or 4 version back, but after that it started giving problems. – Bangash Feb 05 '15 at 21:44
  • BTW do you know any other way, by which we can get code completion without adding any comment block ? – Bangash Feb 05 '15 at 21:45
  • 1
    Not that im aware of from an included/required file like that. I dont recall that ever working but I dont know that I tested it... im usually only worried about object defined in a single file so usually jsut having my method return values defined gets me all the completion i need. – prodigitalson Feb 05 '15 at 21:48
  • @Bangash Oh you might want to check in Preferences > Editor > Code Completion there is a setting there for "All Variables" or "Variables from current file" perhaps you have yours set to the latter - i could see how that could be a sensible default. – prodigitalson Feb 05 '15 at 21:51
  • Wow, so it means they changed the default setting only 3 or 4 version back :) Thank you for the info ! Edit: @prodigitalson I checked, its set to All Variables. – Bangash Feb 05 '15 at 22:11