0

Is there a possibility of including all functions within directory within a class. For example:

class.php

class all_functions{
 foreach(glob(__DIR__.'/functions/*.php') as $file){include($file);}
}

functions/function1.php

function hello(){
 echo 'hello';
}

functions/function2.php

function goodbye(){
 echo 'goodbye';
}

Result would be a class all_functions() that includes functions hello() and goodbye(). I'd like to just add functions into a class by just adding a file with the function.

Maciek Semik
  • 1,872
  • 23
  • 43
  • 2
    You *REALLY* don't want to do this. Be explicit in the code your PHP script should load, based on what it *needs*, because it just takes a single exploit to drop a file in the dir your script is reading from to completely compromise the machine you're going to run this on. And PHP has a *long* history of exploits, with no reason to assume there will never be new ones. If you need it, require it. If you don't need it, you don't need it. Know what your code does: you're about to connect it to the internet. – Mike 'Pomax' Kamermans Jul 11 '23 at 23:11
  • Then why does Autoloading exist and is used often? – Maciek Semik Jul 11 '23 at 23:17
  • 1
    Why don't you just use the existing autoload functionality? See https://stackoverflow.com/questions/7651509/what-is-autoloading-how-do-you-use-spl-autoload-autoload-and-spl-autoload-re for some great explanations of usage – ADyson Jul 11 '23 at 23:22
  • 1
    P.S. A class made of of stuff you just keep adding to from anything in a folder ounds like a bad design (regardless of the above security concerns)... per OO principles a class should be well-defined, compact, have a clear purpose, defined boundary and well-defined interface which doesn't change unless needed. I'm not sure what benefit you think you'll get from separating the components of your class into lots of files? It just sounds like a way to make it a lot harder to read, understand and maintain your code in future, to be honest. – ADyson Jul 11 '23 at 23:25
  • @ADyson but can I autoload functions into one class? My problem is that I can't have two of the same classes. So the solution is to have it under one class with all the functions inside that class. – Maciek Semik Jul 11 '23 at 23:25
  • `My problem is that I can't have two of the same classes`...it's not clear what you really mean by that exactly? What were you trying to do? Yes it's true that you can't have two classes with the same name, but so what? Just make two classes with different names, which describe clearly what each one does. I don't see how this statement leads you to conclude that you ought to create one gigantic class with everything in it – ADyson Jul 11 '23 at 23:26
  • @ADyson sometimes you have to add an extra student to a class or remove one. – Maciek Semik Jul 11 '23 at 23:29
  • ??? You mean like a school class? That has nothing to do with a code class. P.S. I assume your code in the question above is just a meaningless example and doesn't reflect your real functions? Because I feel like this question may be turning in an [XY Problem](https://xyproblem.info/) and if perhaps we could understand the real context, we could suggest a better way overall to solve whatever problem caused you to attempt this task. – ADyson Jul 11 '23 at 23:32
  • @ADyson i'd like to have a class called "connection" with functions that include connections to different mysql databases. Then when I do a PDO call, I can just do new connection linking to the database. And then easily add databases in the future. That's the context. – Maciek Semik Jul 11 '23 at 23:47
  • 2
    @MaciekSemik: And perhaps that's the "issue": You need to better differentiate between code and data. To give an example: You don't pass the functions around (like one function for one connection), but you pass the connection around (and before there is one, the configuration of that connection). Then you can have as many or as little database connections as you want, it's only data that changes (and not code that changes). – hakre Jul 12 '23 at 00:16
  • Agreed. The description wasnt super-clear but in design terms it sounds probably like a case for separate classes with the same interface, each of which can handle interacting with a different type of database. Then you can use whichever is needed at runtime, depending on system configuration or something, and the rest of the code won't care which one it is because the interface is the same. This is like the Dependency Injection pattern, I would suggest you go and read a bit more about things like that. – ADyson Jul 12 '23 at 06:53

2 Answers2

0

I used to have something similar. Generating JS api file on the fly according to php functions. On every request (on dev environment only) I would regenerate it and rewrite the file if necessary.

So, here's pseudo code:

if (DEV) {
    $content = 'class AllFunctions {';
    
    foreach (glob(__DIR__.'/functions/*.php') as $file) {
        $functionContent = file_get_contents($file);
        $content .= "public static $functionContent\n\n";
    }
    
    $content .= '}';
    
    file_put_contents("AllFunctions.php", $content);
}

// later:
include ("AllFunctions.php");

AllFunctions::hello();
IT goldman
  • 14,885
  • 2
  • 14
  • 28
-1

I found a great solution to this problem. Considering everything that was mentioned in the comments, I've devised a solution. Although it deviates from the initial premise, this is a solution I was looking for:

class.php

class all_functions{
 function __call($method,$arguments){
  include 'functions/'.$method.'.php';
  echo $word;
 }
}

functions/hello.php

$word='hello';

functions/goodbye.php

$word='goodbye';
Maciek Semik
  • 1,872
  • 23
  • 43