Dynamically including methods in classes?

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
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Dynamically including methods in classes?

Post by Hades »

Hi

I'm just wondering, in the interests of decreasing maintenance of my data object classes would the following work to include extra methods into the class?

Code: Select all

<?php

class SomeClass{
	
	var $classname = 'SomeClass';
	// Some properties
	
	function SomeClass(){
		//Some code
	}
	
	// Some more methods
	
	include($this->classname . '_extras.inc.php');
}

?>
If not, is there a way I can accomplish this that would remain php4 compatible?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No, you can't have logic or includes outside of class methods, all you can do directly in your class{} block is declare properties really. What's wrong with extensions or plugins? You might want to look into sticking these methods you're referring to into smaller classes and then using a registry of some sort to contain what you need, when you need it :)
User avatar
Hades
Forum Commoner
Posts: 35
Joined: Mon May 08, 2006 4:49 pm

Post by Hades »

Ok.... I have the following breakdown...

The DataObject class is the parent class for an unknown number of child classes.

Each child class will have a specific set of methods that are specific to that child but are directly related to the database table the class is built to interface with.

These child classes can be generated by another script whenever the database schema changes by extracting the database details.

Some child classes may have additional methods that are there for convenience. These convenience methods could not be automatically extrapolated from the database.

The child classes are the ones that have to be instantiated for use in the rest of the application.

This is why I was wondering if it was possible to include additional methods dynamically.
Post Reply