Kazuto_ Posted November 10, 2015 Share Posted November 10, 2015 (edited) hi there, (This is about Helper functions inside PHP OOP) I've been struggling a bit with making functions globally accessible but also retain the features of OOP (e.g $this-> / SomeClass::func / etc) What I want: is to call __aFunction(); anywhere in my project. without having to add prefixes like $this->__aFunction(); or someClass::__aFunction(); But inside the function I DO want to be able to use these features for example: //Main.php namespace Company\Project\Core; use Company\Project\Handlers\Log; use Company\Project\Handlers\Mysql; class Main { public __constructor() { include ("functions.php"); __log_notice("some log"); new MySQL(); } } //Mysql.php namespace Company\Project\Handlers\Mysql; class Mysql { public __constructor() { __log_notice("some log"); } } //functions.php function __log_notice(string $text) { Log::notice(0, $text, true); // DOES NOT WORK, BUT NEEDS TO } I am able to call the function globally. But inside __log_notice() the OOP features like Log::notice(); doesn't work because it's not a class but just an included file. And If i make it a class then the functions can't be called globally anymore but do have the features of OOP. So if anyone has some idea on how to do this, it would be very much appreciated. What I also tried: //Main.php namespace Company\Project\Core; use Company\Project\Core\Common; use Company\Project\Handlers\Log; use Company\Project\Handlers\Mysql; class Main { public __constructor() { new Common(); __log_notice("some log"); new MySQL(); } } //Mysql.php namespace Company\Project\Handlers\Mysql; class Mysql { public __constructor() { __log_notice("some log"); } } //Common.php namespace Company\Project\Core\Common; use Company\Project\Handlers\Log; class Common { public __constructor() { } } function __log_notice(string $text) { Log::notice(0, $text, true); // DOES NOT WORK, BUT NEEDS TO } (Function declaration doesn't matter whether it's outside or inside the class, none works) OR should I go and just use static methods ? Regards. (No useless comments please ) Edited November 10, 2015 by Kazuto_ Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted November 10, 2015 Solution Share Posted November 10, 2015 Just use a normal, plain, simple function. Like in your first example. OOP is about object entities, not about using classes for everything. The reason it doesn't work is because the "Log" that you are use-ing in Main.php does not apply to functions.php too. It's a per-file thing. So //functions.php use Company\Project\Handlers\Log; function __log_notice(string $text) { Log::notice(0, $text, true); }or //functions.php function __log_notice(string $text) { Company\Project\Handlers\Log::notice(0, $text, true); } Quote Link to comment Share on other sites More sharing options...
Kazuto_ Posted November 10, 2015 Author Share Posted November 10, 2015 Just use a normal, plain, simple function. Like in your first example. OOP is about object entities, not about using classes for everything. The reason it doesn't work is because the "Log" that you are use-ing in Main.php does not apply to functions.php too. It's a per-file thing. So //functions.php use Company\Project\Handlers\Log; function __log_notice(string $text) { Log::notice(0, $text, true); }or //functions.php function __log_notice(string $text) { Company\Project\Handlers\Log::notice(0, $text, true); } Thanks yeah I got that but I wanted to get the functionality of OOP and not just in static context als in objective without the need to pass an object through an argument. Thanks for the answer, really appreciated =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.