OOP Quick Question - PHP

In OOP, sometimes you see something like this:

$memberID = $system->members->memberID();

I was interested and completely confused in the part where it is. ->members->...How does it work?

For example, let's say I have a class that I call the called $systems, then how can I put ->members->after it to start the member class?

I only know how to do something like that:

$system = new system();
$memberID = $system->memberID();

But I would like to know how to do this:

$system = new system();
$memberID = $system->members->memberID();

Thank!

- UPDATE - Here's a small update, thanks to everyone who helped me this far! You guys really pointed me in the right direction, I really have a great answer to my own question! :) And thanks to the moderator who edited this question, sorry, I was not familiar with the bbcode syntax.

, - , , → members- > __get(), - "new members()". , , .

, , :

<? class system {

public function __get($name){
    $file = 'lib/'.$name;
    if(file_exists($file)){
        require_once($file);
        $classname = $name;
        $this->$name = new $classname($this);
        return $this->$name;
    }else{
        die('Class '.$name.' could not be loaded (tried to load class-file '.$file.')');
    }
}

}? >

, - , :

$system = new system();
$system->members->functionHere();

lib.

, . , Google, , , !

+3
3

$system $members, , $memberID

$system = new system();
$system->members = new Members(); // or whatever it must be
$system->members->memberId();
+3

- , , -, , - !

, , - ( ).

- . ( , LoD).

LoD ( ).

[EDIT]

:

A) - , ( ). , , , . ! , !

B) - , ! , , , .

[/EDIT]

+1

membersis a property of an object system, as well as an object containing a method memberID().

To assign a property to your object, simply do the following:

class System {
  function __construct() {
    $this->members = new Members();
  }

  // etc
}

or

$systemObj = new System();
$systemObj->members = new Members();

It really depends on the context you want to use :)

As @markus mentioned, properties must be declared public if you access them from the outside. Also, using setters / getters is often much better ...

0
source

All Articles