Reputation: 3531
I have a master class, DBAPI
which contains all the interaction with the database. It's not singleton per se, but is designed to only be instantiated once as $DBAPI
.
When I alter the database, I obviously have to add functions to DBAPI
to let the site use the new functionality, however, since there are a lot of different actions that can be taken, instead of including everything in a single massive class file, I've split them out by functionality/permission level as traits, and the DBAPI
class file is dynamically created by adding traits flagged based off of permission level (read only, read-write etc.). Since the only time the file needs to be created is when new traits are added, I only create the class file if it doesn't exist for that specific user permission level, otherwise I use the already generated file. (If there's a better way to do this I'm all ears).
The issue I'm running into now is that if I add some functions in a new trait, the previously generated classes are obviously not aware of it, and I don't find out about that until I try to use the function in the code somewhere and it fails. It's pointless to write wrappers around every single function call made to check if it is a function first- is there some way to get the DBAPI
class to do some action if code attempts to access a method it can't find?
for example, if code calls some function $DBAPI->newfunction()
$DBAPI handles the exception itself, running some code that will attempt to update itself, which will cause newfunction() to run if it can be found.
Upvotes: 0
Views: 69
Reputation: 16883
(N. B. This architecture has a really bad code smell. I'm sure there's a better way to do this.)
PHP classes can implement the __call
magic method that is used when there is no matching method name.
function __call( $name, $arguments ) {
// Code to run...
}
Upvotes: 1