Page 1 of 1
Adding new Functions() to a Class()
Posted: Sun Jan 18, 2004 3:40 pm
by Gen-ik
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.
Posted: Sun Jan 18, 2004 9:19 pm
by DuFF
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:
Code: Select all
<?php
//start class
class MyFunctions
{
include("databasefunctions.php");
include("useraccounts.php");
// etc, etc
}
$f = new MyFunctions
$f->QueryDatabase($query);
?>
Not sure if it would work or not . . .
Posted: Sun Jan 18, 2004 9:31 pm
by Gen-ik
Nope that doesn't work (which is a shame). It kicks out an error which basically says you can only set variables or define functions which a class{}
Thanks for the idea though.
Posted: Sun Jan 18, 2004 9:44 pm
by Gen-ik
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.
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();
?>
Posted: Sun Jan 18, 2004 11:21 pm
by lazy_yogi
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.
Posted: Mon Jan 19, 2004 12:36 am
by kettle_drum
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
}
Posted: Mon Jan 19, 2004 7:49 am
by Gen-ik
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.
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.
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.
