is_numeric Posted May 22, 2010 Report Share Posted May 22, 2010 ok say I had the following classes QueryClass //<< Runs SQL and query databases LoginClass //<< Does what it says FormProcessClass //<< cleans form data DataFormatterClass //<< Takes query output and creates a Pagination table If I want to access a method from one class to another and possibly another what's the preferred way Example (This is a made up example) I instantiate my FormProcessClass and pass in the $_POST'ed data from a form for cleaning and then return the output. I then need to pass this into my QueryClass which does a db SELECT maybe If the QueryClass returns a result from the SELECT statement then use this output in in my DataFormatterClass and return a nice table of data Do I pass in the objects like this making them available to all other methods $class1 = new FormProcessClass($_POST); $class2 = new QueryClass($class1); $class3 = new DataFormatterClass($class1,$class2); Quote Link to comment Share on other sites More sharing options...
krillz Posted May 22, 2010 Report Share Posted May 22, 2010 You can solve this in very different ways, one could be initiate the class you need in the class, preferably in the costructor so when you call on let say the loginclass who in turn depends on the QueryClass it's all handled by the classes and you do not have to be sending refferer adresses back and forth. class loginClass { private $queryClass = null; public function __construct($param1, $param2 ... ) { $this->queryClass = new loginClass($params) //if required //... code } } Or if you wan't to treat one of the classes as a library you could build a class with static function that do not require you to initite. class queryClass { // variables and what not static public funciton name($param1, $param2 ...) { $var1; $var2; // do what you want return $result //maybe return something } } // then you can call them from everywhere, that is it has access to the class file $catch = queryClass::name then you could build inheritence with the parent class being a static library and calling the functions by parent::Name. It all depends on what you are aiming at and what you find working best. Just try Quote Link to comment Share on other sites More sharing options...
is_numeric Posted May 23, 2010 Author Report Share Posted May 23, 2010 In the library example where do you instantiate the object? Quote Link to comment Share on other sites More sharing options...
krillz Posted May 23, 2010 Report Share Posted May 23, 2010 if you are using a function library made up of static functions, you do not have to instantiate as these are treated as individual modules. Let say like a recipe book where you do not have to buy the entire book but can instead take out chunks of it. If you're using an object that has a dependency of another object then you can instantiate that in the constructer as shown before, then kill it in the destructor. Quote Link to comment Share on other sites More sharing options...
is_numeric Posted May 24, 2010 Author Report Share Posted May 24, 2010 Thanks Krillz I didnt think you needed a destructor since PHP 5 as the objects are disposed of automatically Quote Link to comment Share on other sites More sharing options...
krillz Posted May 24, 2010 Report Share Posted May 24, 2010 Thanks Krillz I didnt think you needed a destructor since PHP 5 as the objects are disposed of automatically Yes they are disposed of, but you have to think of it like this, not always do you have the case that when the script hits the end you are completely fine with losing the info and might want to do something with the object like terminate connections, save data, log or whatever before it completely is dropped from memory. Quote Link to comment Share on other sites More sharing options...
is_numeric Posted May 24, 2010 Author Report Share Posted May 24, 2010 Gotcha Thanks again Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.