Jump to content

Recommended Posts

I'm not certain whether this comes under coding or application design (forgive me if I'm in the wrong area) but I have a bit of confusion the best way to share properties / values across multiple objects that all live within a master object... or for objects to get properties of the object that has called it.

 

For example - index.php creates a new Output() object. The Output class creates instances of various objects. Some of these objects have properties that overlap.

 

The properties of an Input() object that sanitizes and stores all GET / POST / SESSION / COOKIE data, might need to be accessed by a User() object that checks the current user details; or by a URL() object that splits the URL into segments; or by a Database() object that is called to store or retrieve data from the database - which might in turn need to refer to any of the data in the other object.

 

There were three ways I though of doing this:

One - Create once massive class that contains all the methods and functions needed.

This is possible, but probably not in the spirit of OOP!

 

Two - Throw parameters (the old-fashioned way) to the sub-object's methods. (eg. $this->user->login($this->input->sanitize($this->input->getPOST('username'))

This seems safe, but somewhat negates the point of OOP in that there are lots of methods that I would like to be able to refer to the master objects properties without having to explicitly state them!

 

Three - Create a reference to the master Output() object in each sub-object's constructor (eg. global $output; $this->output = & $output).

This appears to work... but I don't know whether it's safe that the sub-object contains the master-object

 

Or do I have my application design complete about-face?

 

This is driving me totally nuts.... Any opinions would be VERY gratefully received!

I think a phpfreaks tutorial has just answered my question...

 

I have now created sub-objects that are aware of the master object by throwing the master object to the sub-object. I had no idea that this was okay... but it seems to work, and I'm happy with it too!

 

I have a lot to learn!  ;D

Your explanation makes me feel like whatever you just did is not OK.

 

While you can pass objects around, child objects aren't supposed to care about their implementation, just use them to accomplish their own goals. Objects should be very selfish.

 

 

$this->user->login($this->input->sanitize($this->input->getPOST('username'))

 

This is a classic case of no! Why should your user object care about how to authenticate? It should only know some basic things, like first name/last name and any real associated attributes. You could then have an Authentication module which accepts a User object to do the login. Here's an example

 

<?php

//model object, which is really just a namespace with accessors
class User {
   private $firstname;
   private $lastname;
   private $username;
   private $password;
   private $userId;

   //associated getFirstName() and setFirstName() methods.. etc 

}

class Authentication {
    public static function authenticateUser(User $user) {
        // do whatever authentication you want done here.
    }
}

?>

 

Next, and this is a minor gripe, but data should be passed around sanitized, and you're going to realize you're going to have

 

$input->sanitize($some_data) 

 

All over your code. You should have a private sanitize method which sanitizes any type of input for consumption in the Input class. That's awesome. But make it transparent and done whenever the data is set.

 

If you have any other questions we'll be happy to answer them.

 

Wow, thanks for the response. I'm learning more here in two days than I have in several years of reading PHP handbooks!

 

What I had done was to create an Input object and a User object in my Main output object, each of which were aware of the properties of the Main output object. So for example (and briefly!):

 

// main output object
class Main {
   public function loadInput() {
      $this->input = new Input($this);
      $this->input->sanitizeAll();
   }
   public function loadUser() {
      $this->user = new User($this);
      $this->user->getSessionUser();
   }
}

 

Then within the User class I had...

 

class User {
   public $username
   public $email
   // plus other properties

   public function __construct(Main $main) {
      $this->main = $main;
   }
   public function getSessionUser() {
      $this->current->username = $this->main->input->session->username;    // getting session data that has been sanitized by the input object;
      $this->current->email = $this->main->input->session->user_email;    // getting session data that has been sanitized by the input object;
   }
   public function login() {
      $this->main->database->newQuery('user');    // creating a new user object within the database object;
      $this->main->database->user->setSql("SELECT * FROM users WHERE username='{$this->main->input->post->username;}' AND password='{$this->main->input->post->password}'")->getResult();
      // then go and set this user's details into the current session etc.
   }
}

 

Now, I understand what you're saying about having a separate user object and authentication class... but I think my confusion arises from deciding the best way to get classes and methods to relate to each other? Am I going about this in completely the wrong way – allowing them to access each other's properties and methods?

 

For example, in the Main class I have a single Database object which represents the database (and connects to it) and I create new Query objects through a "newQuery()" method within the Database object. In your Authentication class would it have to be aware of the database object / connection held in the Main class?

 

Many, many thanks for your help. I REALLY appreciate it!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.