0

here's my problem:

$Me[1] = new User(1);
$Me[2] = new User(2);
$Me[3] = new User(19);
$Me[4] = new User(75);
$Me[5] = new User(100);
foreach ($Me as $k) echo $k->getId();

I'm trying to create 5 users with, of course, different ID. The problem is that the User with ID 2 'overwrite' the User with ID 1, the User with ID 3 'overwrite' the User with ID 2, the User with ID 4 'overwrite' the User with ID 3, the User with ID 5 'overwrite' the User with ID 4.

I don't really know how to fix this, can anybody please help me?

Thanks in advance for your help!


I can't get the $id from the parent class. It seems like the last class initialized "overwrite" all the values (maybe becouse I declared it static? but how can I get it?)..

This is my class:

class User {
    public static $id;

    public function __construct($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }
}

class Sub extends User {
    public function __construct($id) {
    }

    public function printUserLink(){
            echo '<a href="userprofile.php?userid='.parent::getId().'">Go to user profile</a>';
    }
}

How do I get the Id in the "Sub" class?

5
  • Works for me. The error is somewhere else. Commented Jun 25, 2010 at 10:10
  • What is $me? has it been declared ? Commented Jun 25, 2010 at 10:16
  • What is the output of the pasted code? Commented Jun 25, 2010 at 10:17
  • That's a crystal clear case of Multiple Personality :) Commented Jun 25, 2010 at 10:29
  • You need to show your User class definition. Commented Jun 25, 2010 at 11:06

1 Answer 1

1

It's likely that the problem lies in your User class. This simple example works as expected:

class User {
    private $id;

    public function __construct($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }
}

$Me[1] = new User(1);
$Me[2] = new User(2);
$Me[3] = new User(19);
$Me[4] = new User(75);
$Me[5] = new User(100);

foreach ($Me as $k) echo $k->getId() . "\n";

Outputs:

1
2
19
75
100

Without details of your User class (as suggested by ircmaxell), resolving the problem will be tricky.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't get the $id from the parent class. It seems like the last class initialized "overwrite" all the values (maybe becouse I declared it static? but how can I get it?).. This is my class: class User { public static $id; public function __construct($id) { $this->id = $id; } public function getId() { return $this->id; } } class Sub extends User { public function __construct($id) {} public function printUserLink(){ echo '<a href="userprofile.php?userid='.parent::getId().'">Go to user profile</a>'; } } How do I get the Id in the "Sub" class?
Yes, it will be because you have declared it as static (see Static Member Variables for an use of static member variables). Why do you need it declared as static?
Becouse I don't know how to access it from a subclass (parent::$var doesn't work..) How can I do?

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.