views:

52

answers:

1

Hello fellow PHP geeks, I'm sort of in a weird situation... One of which I've never come into before (It's maybe due to my break from PHP to .Net). Using the framework CodeIgniter.

Well here's the situation... I've got a User class which acts as the user object containing username/password/email etc... (shown below):

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    include_once(APPPATH . INTERFACE_PATH . 'IUser' . EXT);
    include_once(APPPATH . CLASS_PATH . 'Encryption' . EXT);

    final class User extends Encryption implements IUser
    {
        private $_username;
        private $_password;
        private $_email;

        public function SetUserData(StdClass $data)
        {
            $this->_username = $data->Username;
            $this->_password = self::EncryptPassword($data->Password);
            $this->_email = $data->Email;
        }

        public function Username()
        {
            return (string)$this->_username;
        }
        public function Password()
        {
            return (string)$this->_password;
        }
        public function Email()
        {
            return (string)$this->_email;
        }
    }

?>

Now nothing exciting so far... However once this has the values in etc... one should think using this class elsewhere will be simple... DOHHH well it isn't

Here's where it's being used:

<?php
#some class/methods up here...

    public function AddNewUser(User $user) #here is where it's passed in...
    {
        print '<pre>';
        print_r($user);  # my output of this is shown in email.
        print '</pre>';
        return;

        $user->Username = $this->db->escape($user->Username);
        $user->Password = $this->db->escape($user->Password);
        $user->Email    = $this->db->escape($user->Email);

        $sql   = 'CALL sp_RegisterUser(' . $user->Username . ', ' . 
                    $user->Password . ', ' . $user->Email . ', '  
                    . (int)((isset($user->RefId) && is_numeric($user->RefId)) 
                    ? $user->RefId : 0) . ', @usersId);';
        $query = $this->db->query($sql);

        if($query)
        {
            $sql   = "SELECT @usersId;";
            $query = $this->db->query($sql);

            if($query->num_rows())
            {
                $sqlData = $query->result();
                $query->free_result();

                foreach($sqlData[0] as $k => $v)
                {
                    $array[$k] = $v;
                }

                return (int)$array['@usersId'];
            }
        }
    }
}

Now the print_r outputs the following:

User Object
(
    [_username:private] => joebloggs
    [_password:private] => m/SzYRxTF29cZJqk/B/2sg==
    [_email:private] => [email protected]
)

How come the methods are now called the variables being returned but with their value?

Confusing times...

Any help is much appreciated.

Thanks, Shaun

PS, I've also included the class/interface in the other library so there's no excuse of the object not knowing how it should render.

+4  A: 

Remember that your class has properties and it has some getter methods. The properties are the private variables declared at the top of the class, for example private $_username;, and the getters are the methods declared below, such as public function Username(). The getter Username() returns the value of the property $_username.

Firstly this naming is pretty nonstandard. At least, the way I would name my getters is like getUsername() or getPassword(). This might start to help you understand your problem, by differentiating between the getters and the properties.

When you use print_r() on an object, it prints the current state of the object. The current state of the object is defined by the type of the object and the values of it's properties. As you see, in your print_r() output, it says that it's a User object with 3 private properties, _username, _password and _email. This is correct and it has the correct values associated with them. print_r() is doing what it's supposed to.

But then the problem with the code following is how you're trying to access these properties. Let's look at the lines:

$user->Username = $this->db->escape($user->Username);
$user->Password = $this->db->escape($user->Password);
$user->Email    = $this->db->escape($user->Email);

This is not right. Firstly, to get the properties from the user you would use: $user->Username() or $user->getUsername() if you changed the naming convention. Remember, these are functions so must be called by placing parentheses after the name of the function. They are functions that return the values from inside the object.

And then you're trying to set the values inside object incorrectly. For this you need to create some setter methods, like setUsername(). So those lines would become:

$user->setUsername($this->db->escape($user->getUsername()));
$user->setPassword($this->db->escape($user->getPassword()));
$user->setEmail($this->db->escape($user->getEmail()));

But this is probably not the approach you really want to go for. Why don't you escape the properties when you set them in the constructor of the class? Or escape them before you pass them to the class? It doesn't really make sense to pull them out of the class, escape them, then push them back in.

Hope this stuff helps.

sixfoottallrabbit
Hello sixfoottallrabbitm, thank you for the solution. The reason I put it into the class was to handle the database escaping/queries in another class/model but the main values being stored/kept from the other.I'm always open on better solutions into programming as I'm forever learning.But thank you for your solution.Shaun
Shauny