Jump to content

Expose objects to other classes


is_numeric

Recommended Posts

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

Link to comment
Share on other sites

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

 

 

 

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...