I'm developing a club membership register web application and I am a fairly newbie when it comes to oop. The problem I'm having is that I would need to call a function outside a class, but I know you can't do that in PHP and I need to solve it somehow. Show
This code will demonstrate what my problem is: members.php
I've written the code for database actions in a separate file called database_functions.php, but I can't include that file inside a class. What I need to know is how can I access those functions without having to write them again inside the class? (I have written applications before with VB.Net and in it I can use functions of the current project inside classes.) asked Jul 7, 2014 at 7:11
2 You can extend a class to another class to access parent class functions and properties in subclass. Functions declared private cannot be accessed like that. You can also pass object of another class to to one class as an argument to the constructor. if you have a class name parent :
Then you can access its functions if you extend it to your class :
OR
Then in child class you can use :
Then within other functions in child class you can access the parent class methods using
answered Jul 7, 2014 at 7:30
MijoeMijoe 2082 silver badges9 bronze badges you can use
answered Jul 7, 2014 at 7:27
Mobasher FasihyMobasher Fasihy 9912 gold badges8 silver badges17 bronze badges If you have a main file that calls different files (via At the top of your class file, for instance, you could call another file with use of The other options is to wrap the other functions you have inside another class, provided that it makes sense. Class A could then hold a field with a class B object and call
The benefit of this is that everything is abstracted in classes. Once you're outside of the class, all you have to know is what methods (functions) you can use of a class and you won't have to worry about the implementation details. A direct advantage of this is that you don't have to pass the same parameter to same functions all the time, say, if you need a database handler to execute SQL code within a function. A class could just hold such an instance and you won't have to repeat the same parameter all the time. An additional advantage is that your global namespace won't be polluted with specific functions. answered Jul 7, 2014 at 7:28
ljacquljacqu 2,0821 gold badge15 silver badges21 bronze badges I'm not sure if you include the file which defined the functions you want. I tried this with my symfony preject and it works well. The test.php contains a function.
and I include it in the Model:
and it output whith 10. Maybe you need the include the file I think. answered Jul 7, 2014 at 7:37
bixiaopengbixiaopeng 3532 silver badges11 bronze badges In my opinion here is one solution :-Make your file('database_functions.php') a class containing all your functions. Like..
Now when you are writing your class which is named as "member", write it in such a way that it extends your mydbcls.
now you can access your db functions this way
answered Jul 7, 2014 at 7:50
Not the answer you're looking for? Browse other questions tagged php mysql oop or ask your own question.How do you call a function outside the class in PHP?you can use require_once() with exact path of that class after that you should use the object of the included class and function name for calling the function of outer class.
Can classes use functions outside the class?Yes, definitely you can call class non-member function from class.
Can I define function outside class in Java?Unlike C++, in Java, we cannot have just function declarations in the class and definitions outside of the class.
|