简体   繁体   中英

Quick OOP Question - PHP

In OOP, sometimes you see something similar to this:

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

I was wondering and totally confused on the part where it is ->members->... How does that work?

For example, lets say I have a class that I call up called $systems , then how can I put ->members-> after it to run the members class?

I only know how to do something along the lines of this:

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

But I would like to know how to do this:

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

Thanks!

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

I wanted something to automatically make the new classes, for example calling ->members-> would be automatically included using __get() rather then having to do manually put in something like "new members()". A little difficult for me to explain, but I hope you got the basics of it.

Anyhow, here is the code that I use:

<? 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.')');
    }
}

} ?>

Now, if I were to do something the lines of this:

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

It would automatically create a new instance of the members class and require the file from the lib folder.

If this is against the rules then I apologize. I just wanted to post this for anyone who came across this question while searching Google, because I know I always land up here when googling things!

the $system variable holds an object which has a property named $members which itself holds an object which has a property $memberID

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

When you see something like that, you know that someone has most probably done something wrong!

In order for this code to work, you need to grant public access to a member variable of an object (the former storing an object).

To grant public access to such a member variable is in most cases bad practice. The variable should only be accessible through a getter (at least, it will still violate the LoD).

This code breaks the principle of encapsulation and the LoD (Law of Demeter).

[EDIT]

Why it is almost certainly a mistake:

A) Granting direct public access to member variables is in most cases a mistake, because it makes the public interface of your class rigid (hard to change). If you have a getter, you can change the implementation of the member anytime, the getter will still be the same and you don't need to change the call from anywhere. You can NEVER write a proxy for direct access to a variable! Writing a proxy for a getter on the other hand is easy!

B) Granting direct public access to member variables is in most cases a mistake, because you let everyone talk to a class inside a class directly! This will most probably lead to higher maintainance costs when the public interface of any of these two classes changes.

[/EDIT]

members is object property of system and is also an object that contains method memberID() .

To assign property to your object, simply do something like this:

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

  // etc
}

or

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM