0

I'm using this PHP code for creating PHP class with default single variable. But it's not work perfectly.

Note : I need to store data in single variable.

<?PHP

class hello
{
    public $data;
}

$test = new hello();

$test->data->user1->name = "Charan";
$test->data->user1->sex  = "male";
$test->data->user1->age  = "25";

$test->data->user2->name = "Kajal";
$test->data->user2->sex  = "female";
$test->data->user2->age  = "21";


print_r($test->data->user1);


?>

I got this error :

enter image description here

Please help me , how to fix it ?

4
  • 1
    How about using arrays instead of objects? Commented Dec 24, 2012 at 8:28
  • You didn't initialize user1 and user2 Commented Dec 24, 2012 at 8:29
  • 1
    @Shivan Raptor: ... and data Commented Dec 24, 2012 at 8:29
  • sorry i deleted my comment. its my fault Commented Dec 24, 2012 at 8:31

2 Answers 2

4

You have only declared the variable, but you havent set it up as a "type". For example youre trying to assign values to properties n an object... but $data is not an object - its not anything. You need to either assign an object to $data in the constructor for the class, or assign it from outside the class.

Constructor:

class hello
{
    public $data;

    public function __construct() {
        $this->data = new StdClass();

        // assuming you want the users set up here as well
        $this->data->user1 = new StdClass();
        $this->data->user2 = new StdClass();
    }
}

From Outside:

// assume we are use the same class definition from your orginal example
$test = new hello();
$test->data = new StdClass();
$test->data->user1 = new StdClass();
$test->data->user2 = new StdClass();

$test->data->user1->name = "Charan";
$test->data->user1->sex  = "male";
$test->data->user1->age  = "25";

$test->data->user2->name = "Kajal";
$test->data->user2->sex  = "female";
$test->data->user2->age  = "21";
Sign up to request clarification or add additional context in comments.

4 Comments

"its not anything" --- it's actually not anything but NULL
oh! it's not make any effect... :-(
@user1882503: may be it's too early for objects then? How about arrays?
@zerkms: HA! I never thought they were actually null... Im used to seeing people define explicitly like protected $something = null; if they want it null so always figured there was a difference.
2

If you want your class flexible, $data variable inside hello class must be an array.

For example:

<?php

class Person{

public $data = array();

function __construct(){}

}

$person = new Person();
$person->data['name'] = 'Juan Dela Cruz';

echo $person->data['name'];

?>

2 Comments

Why do you need an empty constructor?
Its a template from my editor, it creates a default empty constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.