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 )