best way to call function from an extended class
Asked Answered
C

2

0

I am using phpstorm as IDE. And in my class.php, I started the class as

class MyClass extends Database{
    function sample(){
        $this->query();
    }
}

the query() is in the class Database. But phpstorm shows a warning that

Method 'query' not found in class MyClass. Referenced method is not found in subject class. 

But the function is working without any problem.

Is there any problem with this code style? Or Do I need to try a different approach? I searched many websites. But didn't get a proper answer. Please help. Thank you.

Congruity answered 10/10, 2014 at 9:11 Comment(5)
Is this the actual piece of code that shows the error?Safford
Sorry for that missing of information. That code is working perfectly. I edited that in code now. :)Congruity
$this->query(); should be inside of a method.Kliman
Without screenshots / proper code we will not be able to help you. Because right now it looks like you do not know how PHP works (based on code sample you have provided).Safford
@MarvinSaldinger, yeah. sorry for thet. Missed to add that. But in my actual code, it is with in a method. and is shows the warning.Congruity
M
0

The problem is you need everything in classes to be called from a method or outside of the class after the class is instantiated.

you cannot use $this outside the method scope in a class

EDIT, OP changed question :

works fine for me, no warning enter image description here

Magda answered 10/10, 2014 at 9:20 Comment(2)
It is not an error. Just a warning by PHPStorm. If I hover on query(), it shows the messageCongruity
Try to invalidate your cache and restart phpstorm, it might be a caching/index problemMagda
K
0
class MyClass extends Database{
   function sample(){
       parent::query();
   }
}

Is this working?

Kliman answered 10/10, 2014 at 9:23 Comment(4)
now the warning is the previous error in the question, and it shows one more warning Multiple definitions exist for class parentCongruity
@Congruity Which means that you have multiple (more than one) Database classes in your projectSafford
How it could be possible? multiple class with name name will cause error while execution. Right? Here no such things happenedCongruity
@Congruity "How it could be possible?" Check your project -- how do I know how did you set it up and what libraries you are using? "multiple class with name name will cause error while execution. Right?" Only if you import both of them. Otherwise no. But from IDE point of view it does not know which class to use.Safford

© 2022 - 2024 — McMap. All rights reserved.