views:

188

answers:

3

Hi,

I have three classes MainClass, Member, and Project. Member and Project extend the MainClass.

First I create a MainClass object, then a Member object and execute the function setMember:

$mainclass = new MainClass();
$member = new Member($id);
$mainclass->setMember($member);

When the $member variable is set to the current member I want to use this variable in my Project class but I can't get it to work :s

I need the member's id so in theory there are two possibilities:

mysql_query("INSERT INTO projects (title, user_id) VALUES('$title', ".$this->member->id.")");

or

mysql_query("INSERT INTO projects (title, user_id) VALUES('$title', ".$this->member->getId().")");

The first one, $this->member->id, results NULL

The second one, $this->member->getId(), gives me this error:

Call to a member function getId() on a non-object in ....

Here are my classes (I stripped most of it offcourse)

class MainClass{
    public $member = NULL;

    public function __construct(){}

    public function setMember($member)
    {
        $this->member = $member;
    }

    public function getMember()
    {
        return $this->member;
    }
}

class Project extends MainClass{
    public $id;

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

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

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

    public function addProject($title){
        mysql_query("INSERT INTO projects (title, user_id) VALUES('$title', ".$this->member->getId().")");
    }
}

class Member extends MainClass{
    public $id;

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

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

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

Instead of creating an instance of MainClass, you should be creating an instance of Project.

$mainclass = new Project();
$member = new Member($id);
$mainClass->setMember($member);
jimyi
I do create an instance of Project but its not relevant to my question so I didn't post it I need to be able to access the member object that I set in MainClass via Project
Bundy
using "new Project" instead "new MainClass" and adding $mainclass->addProject('foo'); your example code is working for me without errors/warnings/notices.
VolkerK
A: 

You have two children extending a parent but the children classes can't "see" each other private or public. You need to import the data into the Project class.

Eddy
+2  A: 

Take a step back and look at the relationships between your classes. In your database, a Project "has a" Member(or 'user' in the DB).

Your class implementations do not reflect this relationship. MainClass "has a" Member, but Project does not. You're using a member of the base class to create this relationship.

Unless $member in MainClass is static, an instance of Project can't see what another instance of MainClass has set for it, because they're separate instances. I would not recommend making $member static, because you'd essentially be using it like a global variable.

Like I said, take a step back and look at the relationships. Do they generally follow the relationships in your database schema? Why do Member and Project derive from MainClass? What is their common behavior?

I realize I'm asking more questions than I'm answering, but my gut reaction is that this particular technical issue isn't your real problem-- it's that your class design needs to be rethunk a little bit.

Hope that helps.

pjbeardsley
I would add that he has pretty much failed at design. Parent shouldn't work with children, as child himself has everything parent has, but he passes child (member) to parent. He could do the same operation inside child himself.
usoban
I don't really understand why I failed. These classes are merely an example. I just want to use the member object, that I set in my main class, in the child class Project or isn't this possible?
Bundy
I think you're having trouble with the difference between classes and instances.Member and Project both derive from MainClass. If you create an instance of Member, and then you create an instance of MainClass and call setMember(), now only that PARTICULAR instance of MainClass has it's $member field set-- any other instance of MainClass, Project, or Member will NOT be able to see that field, because they all have their own "personal" copy and it's not set.To do what you're trying to do, you'd need to create an instance of Project, call it's setMember(), then addProject() should work.
pjbeardsley