Jump to content

Global functions in OOP with class support


Kazuto_
Go to solution Solved by requinix,

Recommended Posts

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 :pirate: )

Edited by Kazuto_
Link to comment
Share on other sites

  • Solution

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);
}
Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.