N
N
Nikolay2016-08-31 17:20:26
PHP
Nikolay, 2016-08-31 17:20:26

OOP why return the value of a non-inherited class?

class Member {

    private $username;

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

    public function getUsername() {
        return $this->username;
    }
}

class Topic {

    private $member;
    private $subject;

    public function __construct( $member, $subject ) {
        $this->member = $member;
        $this->subject = $subject;
    }

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

$aMember = new Member( "fred" );
$aTopic = new Topic( $aMember, "Hello everybody!" );
echo $aTopic->getUsername();    // Displays "fred"

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2016-08-31
@zzzmaikzzz

That's right, you're calling a third-party class method, why shouldn't it work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question