2

I'm working on PHPStorm and changing a PHP project from procedural code to OOP, however, when changing to OOP PHP doesn't seem to find objects' methods, here's a picture

problem

how do I solve this? Or should I just ignore it?

1 Answer 1

3

that is because you should use

/**
 * @var RegistrationDB 
 */
var $db;
/**
 * @var Notifier 
 */
var $notifier;

function __construct() {
    $this->db = new RegistrationDB();
    $this->notifier = new Notifier();
}

in the constructor,

if (!$this->db->hasUser($email)) ... 

when you try to reference the property (underpinned by the var $db).

EDIT : added phpdoc pragmas to render the property's class resolvable.

EDIT 2 : in php OOP, the object's properties are declared by a var. So, var $db; implies that a db property exists for the classes' instance objects

var $db; in class Foo

...

$a = new Foo();
$a->db;  // correct
$a->$db; // very probable runtime error

for example, from one of my current projects :

$clinic = \DAO\clinics::insert( true , 'Funky Clinic inc' , new DateTime('now'));
$clinic->active = true;
$logger->info($clinic);
$clinic->$active = false;  // <- this is line 20 from the stack trace

The third line will log properly the object to my log file, while line 4 will bomb with the following error in the console :

> php TestPatient.php 
PHP Fatal error:  Cannot access empty property in /Users/yvesleborg/devel/Shiva/MyClient/HisProject/DAO/SQL/v1.0/TestPatient.php on line 20
PHP Stack trace:
PHP   1. {main}()     /Users/yvesleborg/devel/Shiva/MyClient/HisProject/DAO/SQL/v1.0/TestPatient.php:0
Sign up to request clarification or add additional context in comments.

3 Comments

nevermind, restarting phpstorm seemed to do the trick. thanks anyway man, +1 EDIT: can you explain whats the difference between "$this->$something" and "$this->something"?
well, whatever fixed the issue then. Still beware of $this->$something, you may get some pretty funky runtime errors at runtime. Prefer $this->something when referencing properties declared with a var. I also edited my answer to make the class very explicit.
@DarkW ... added concrete example of proper referencing for object's properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.