2 Replies - 432 Views - Last Post: 12 October 2012 - 11:10 PM Rate Topic: -----

#1 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Setting an array properly in class?

Posted 12 October 2012 - 08:41 PM

So now I'm learning about classes and I'm trying to create an array to hold all user data, for example:

user.class.php

<?php

class User
{
    private $data;

    public function __construct(Array $properties=array())
    {
        $this->data = $properties;
    }
    public function __set($property, $value)
    {
        return $this->data[$property] = $value;
    }
    public function __get($property)
    {
        return array_key_exists($property, $this->data) ? $this->data[$property] : null;
    }
    function register()
    {
        var_dump($this->data);
    }
}

?>


process_register.php

<?php

include 'user.class.php';
$user = new User(array($_POST['username'], $_POST['password'], $_POST['email']));
//testing setter
$user->data = $_POST['username'];
$user->data = $_POST['password'];
$user->data = $_POST['email'];
$user->register();

?>


index.php Just in case you wanted to know

<html>
    <body>
        <form name="register" action="register_process.php" method="post">
            Username: <input type="text" name="username" />
            Password: <input type="password" name="password" />
            E-mail: <input type="text" name="email" />
            <input type="submit" value="Register" />
        </form>
    </body>
</html>

<?php
    //insert code here
?>


The problem is that while creating the object I can only add one of the values when trying to set... perhaps I'm doing it wrong but how can I set multiple values to the $data array? As of right now I only have 4 values: username, password and email created by the constructor, than email again added by the setter.

I just need a little direction. Thank you for your time.

*EDIT* I meant to say "Setting an array properly in class?" oops...

This post has been edited by kiasta: 12 October 2012 - 08:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Setting an array properly in class?

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2999
  • View blog
  • Posts: 10,384
  • Joined: 08-August 08

Re: Setting an array properly in class?

Posted 12 October 2012 - 09:18 PM

Actually, if $data is private you can't add anything to it the way you're attempting. Check this out:
<?php
class test {
	private $one;
	public $two;
	
	function set_one($value) {
		$this->one = $value;
	}
	
	function get_one() {
		return $this->one;
	}
}

$obj = new test();

$obj->one = 10;

echo $obj->get_one();

Now try:
<?php
class test {
	private $one;
	public $two;
	
	function set_one($value) {
		$this->one = $value;
	}
	
	function get_one() {
		return $this->one;
	}
}

$obj = new test();

$obj->set_one(10);

echo $obj->get_one();

You can do the same with an array:
<?php
class test {
	private $one;
	public $two;
	
	function set_one($value) {
		$this->one = $value;
	}
	
	function get_one() {
		return $this->one;
	}
}

$arr = array("A", "B", "C");

$obj = new test();

$obj->set_one($arr);

$result = $obj->get_one();

print_r($result);

Was This Post Helpful? 3
  • +
  • -

#3 kiasta  Icon User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 257
  • Joined: 18-November 07

Re: Setting an array properly in class?

Posted 12 October 2012 - 11:10 PM

I took your advice and changed my setter up a bit, I'm not sure why I had it that way... anyways here is the revised code and it works perfectly:

index.php

<html>
    <body>
        <form name="register" action="register_process.php" method="post">
            Username: <input type="text" name="username" />
            Password: <input type="password" name="password" />
            E-mail: <input type="text" name="email" />
            <input type="submit" value="Register" />
        </form>
    </body>
</html>

<?php
    //insert code here
?>



process_register.php

<?php

require 'user.class.php';
$user = new User(array($_POST['username'], $_POST['password'], $_POST['email']));
//testing setter
$user->set(array($_POST['username']));
$user->set(array($_POST['password']));
$user->set(array($_POST['email']));
$user->register();

?>



user.class.php

<?php

class User
{
    private $data;

    public function __construct(Array $properties=array())
    {
        $this->data = $properties;
    }
    public function set($value)
    {
        $this->data[] = $value;
    }
    public function get($property)
    {
        return array_key_exists($property, $this->data) ? $this->data[$property] : null;
    }
    public function register()
    {
        var_dump($this->data);
    }
}

?>



Thanks a lot for the help, it is much appreciated! :)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1