Jump to content
  • Advertisement
Sign in to follow this  

[web] [php] Best way to associate a class with another?

This topic is 4145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What's the best way to associate, or attatch, one class to another? As an example, you'd have an object, check its class, and somehow get the other class associated with it. I'm doing this for Thacmus, to add 'searchability' to the Field classes (found in /field.php, v0.1.2a is the latest). However, I haven't yet decided/implemented a way to do this. For the method, I don't want to have to change anything in the main class I'm associating the other with. Keep in mind I don't want to associate it with objects, but with classes. I need to do this because you might not just be searching for values, i.e., you could be searching for a range of dates, or a set of options. Per technique, it would take the fields for the SearchForm, and find the associated class, and instantiate an object of it using eval(). Then, the form would be rendered, the submission handles, yadda yadda yadda. The actual search process isn't a concern just yet, but getting the association down. A couple of techniques I thought of: > Creating an array of classnames, having the Search name as the index, and the class(es) it is associated with. Inheritance could be a problem here - maybe a few workarounds could fix that, using parent checks or something, hopefully only having to use names instead of objects. I'm most likely to go with this one. [This technique is non-invasive: nothing in the main Field classes needs to be changed] > Defining a static per Field class that connects it to the SearchField (and then getting the highest-level static according to the object's class). This would handle inheritance better, just how scoping is done with statics in classes. [Invasive: requires stuff for the field to be changed] [Edited by - deadimp on June 16, 2007 9:11:01 PM]

Share this post

Link to post
Share on other sites
Per technique, it would take the fields for the SearchForm, and find the associated class, and instantiate an object of it using eval().

Start brushing up on PHP :-) Oh, and stay away from eval() if you can. Variable functions/classes and call_user_func() are your friend. A suggestion:

<form method="post">
Search for: <select name="class">
<option value="SomeClass">Something</option>
<option value="SomeOtherClass">Something else</option>

$class = $_POST['class'];

if (!class_exists($class))

$instance = new $class; // PHP4
$instance = new $class(); // PHP5
$result = $instance->do_search();

Share this post

Link to post
Share on other sites
I didn't know you were able to apply that to new-calls - I had assumed that from one of the posts on the PHP manual, where it couldn't use a variable for a class's parent or something or other.

As for the layout for searching: It isn't going to do search-types (as a date, string, etc) by user input, but the Field's that are set in the form's field list. Despite that, it's still the same concept.
Here's the run-down of the structure, with the first technique I described:
//All fields inherit from this, such as FieldText, FieldDate, ...
class Field {
function render($value);
function submit($in,&$out); //process() would make more sense, but I'll stick with submit()
function disp($value);
class Form {
var $field=array(); //Array of Field's
//Set by ctor
//Different file - The search stuff
class SearchField extends Field {
//Same structure as Field but with a little more, and something to generate conditions for MySQL
static $assoc=array(); //Array of associations
//Search field definitions
class SearchText {
//Blah blah
//Associate class with the corresponding Field
//I might switch the order to: SearchField::$assoc["FieldText"]=SearchField::$assoc["..."]="SearchText"; - Just to make searching for the search field easier
//And if I were to do that, I'd definitely make a function for it
class SearchForm extends Form {
//Takes normal fields as an argument, then finds the corresponding search field for them, then renders as any other form - May also be used to piece together the MySQL statement... I don't know yet

Thanks for the tips, especially that on new.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!