Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics Building a Collection Enhancing the Collection

rob111
rob111
8,379 Points

Why are we not using the __get() and ___set() Magic Methods?

Throught this course we are using setTitle() and getTitle() methods for example but isn't that what these Magic Methods are for? Am I missing something?

Why are we not using the __get() and __set() Magic Methods?

Boon Kiat Seah
Boon Kiat Seah
66,664 Points

need to look into this questions too. Have this on my mind as well.

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hey Rob,

No, that's actually not what __get() and __set() are for. Check out this StackOverflow answer. These magic methods are for use when you cannot access the property directly (i.e. it's a private property out of scope). You should typically be using actual getter and setter methods like setTitle() and getTitle().

Cheers :beers:

-Greg

rob111
rob111
8,379 Points

Greg Kaleka can you shed some light on this?