Adding new Functions() to a Class()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Adding new Functions() to a Class()

Post 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.
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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 . . .
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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();

?>
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Having the classes inherit from each other would be a lot easier and nicer code.

Code: Select all

class A &#123;
   #blah
&#125;

class B extends A &#123;
   #blah
&#125;

class C extends B &#123;
   #blah
&#125;
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.

;)
Post Reply