Loading video...
More Magic Methods
- In PHP, any methods starting with
‘__’are known as magic methods. - Magic methods have special functionality in the PHP language and are called automatically by PHP.
- Object methods should not be given the same name as any PHP magic methods.
- A destructor is an object method called automatically by PHP when an object is no longer being used or
it has been unset (or set to
NULL). They are used to ‘clean-up’ the state of an object before its destruction, such as closing any open file resources. - In PHP, destructors are magic methods and have the name
__destruct(). __toString()is a magic method that is automatically called by PHP anytime an object variable is used in a string context. By defining this method, you can explicitly set how objects should be represented in string form.- In PHP, object properties and methods have a visibility associated with them that describes where they can be accessed from.
- An object property or method can have one of three visibility values, known as access modifiers:
public– accessible inside OR outside class definitionprivate– accessible inside class definition ONLYprotected– accessible inside class definition OR by inherited classes (not covered in this course)
__get()is a magic method that is used to access inaccessible, orprivate, properties from code outside of a class’s definition.__set()is a magic method that is used to set inaccessible, orprivate, properties from code outside of a class’s definition.- An alternative means of getting/setting
privateobject properties is to create public getter and setter methods that provide the ability to get and set an object’s private properties. For example:- Getter for a 'lastName' property:
public function getLastName() {... - Setter for a 'lastName' property:
public function setLastName($lastName) {...
- Getter for a 'lastName' property:
- Additional Resources:
More Magic Methods
Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.
- Intro
- Lesson Overview
- Magic Methods
- Destructors
- _to String ()
- Access Modifiers
- Introduction to Access Modifiers
- Access Modifiers: Public
- Access Modifiers: Private
- Access Modifiers: Protected
- Object Properties and Methods
- Coding Example: Public Access Modifiers
- Coding Example: Private Access Modifiers
- _get()
- _set ()
- Using Getters & Setters
- Homework Challenge
- Homework Challenge (cont.)

































Start Learning Now
Our free lessons will get you started (Flash® 10 required).
Sign up for Educator.comGet immediate access to our entire library.
Features Overview