I've currently got a few different PHP pages which contain various functions, one for MySQL another for user accounts and so on.
Is it possible to load all of the functions from the various pages into one class so that I could access them with the usual $myClass->functionX ?
The functions are in different files because it makes them easier to manage.
Adding new Functions() to a Class()
Moderator: General Moderators
I don't know how your functions are seperated or layed out, so this suggestion may or may not work. Here is what I'm thinking:
Not sure if it would work or not . . .
Code: Select all
<?php
//start class
class MyFunctions
{
include("databasefunctions.php");
include("useraccounts.php");
// etc, etc
}
$f = new MyFunctions
$f->QueryDatabase($query);
?>Aha.. I've finally come up with a way of doing it.
The following code works by using eval to 'stick' the functions and the class together.
The following code works by using eval to 'stick' the functions and the class together.
Code: Select all
<?php
ob_start();
require_once("functionsA.php");
require_once("functionsB.php");
$funcs = ob_get_contents();
ob_end_clean();
eval("class myClass { ".$funcs." }");
$o = new myClass();
$o->call_a_function_from_A();
$o->call_a_function_from_B();
?>Oh dear god man. That's not OOP. Thats proceedural with classes.
ie. this is a pointless way to use classes. There is not a single thing dofferent to just including those functions in an include file.
You basically removed every single benefit of OOP that exists.
How on earth you did that in one foul swoop is both a miracle and a tragedy to be marvelled at.
Please incinerate the computer you wrote that on and try to never do it again.
ie. this is a pointless way to use classes. There is not a single thing dofferent to just including those functions in an include file.
You basically removed every single benefit of OOP that exists.
How on earth you did that in one foul swoop is both a miracle and a tragedy to be marvelled at.
Please incinerate the computer you wrote that on and try to never do it again.
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
Having the classes inherit from each other would be a lot easier and nicer code.
Code: Select all
class A {
#blah
}
class B extends A {
#blah
}
class C extends B {
#blah
}Hmm. I'm not just adding a load of random functions to a class. All of the functions are part of the same class (and it's a BIG class) but I've broken the functions down into seperate files so that they are easier to manage.lazy_yogi wrote:Oh dear god man. That's not OOP. Thats proceedural with classes.
ie. this is a pointless way to use classes. There is not a single thing dofferent to just including those functions in an include file.
You basically removed every single benefit of OOP that exists.
How on earth you did that in one foul swoop is both a miracle and a tragedy to be marvelled at.
Please incinerate the computer you wrote that on and try to never do it again.
I could have them all in one file so that I wouldn't need to 'stick' them together again but as I said I've done it to make managing the class easier than it would be in one big lump.