0

I am learning about Magic Methods in OOP, after searching Stackoverflow, php.net and all over Google I've found that the explanation on the similarity of these two methods are a bit vague.

The biggest question is regarding __isset, because __get in itself is quite straightforward.

I understand __get() as: "__get() is utilized for reading data from inaccessible properties."

Which works in the below method and when calling a non-existent property it explains clearly what it does.

public function __get($propertyName) {
      echo "<hr>attempted to read non-existing property: $propertyName<hr>";
 }

But then what about __isset()? If you already have a __get() magic method in place, would there still be a use for __isset()?

While writing this, I'm thinking that I could implement __isset() in a class if I'm gathering data from $_POST just like how I'd use isset() outside a class. Would that be the difference?

I have checked What is the difference between isset() and __isset()? and unfortunately that doesn't seem to answer my question.

Manmoth
  • 13
  • 3
  • I suggest you make some small sample programs to really solidify your knowledge. You may still need __isset() even if you have a working __get(). They do different things. – ryantxr Aug 21 '17 at 18:27
  • 1
    `__isset()` should always return a boolean. `__get()` should return the actual value. Completely different use cases. – rickdenhaan Aug 21 '17 at 18:28
  • Seriously. The questions you pointed to is a complete discussion on exactly what these functions do and why you use them. – ryantxr Aug 21 '17 at 18:32
  • Yeah, it is a discussion on what these function do, but to be honest the bit I was looking for wasn't in that discussion. The question that if you have __get(), would you still have __isset() in the same class as at first it seemed like the outcome of these methods would be the same. These comments helped me realise that that's not the case. – Manmoth Aug 21 '17 at 18:39

0 Answers0