Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Along with "Magic Methods", PHP also provides a large number of predefined constants to give us... a little more magic. These "Magic Constants" can provide details about our script and our environment. We'll look at a few "Magic Constants" that help us understand our classes and objects.
Magic methods can be a lot of fun,
as well as extremely powerful.
0:00
But PHP gives us even more magic
in the form of magic constants.
0:05
PHP provides a large number of predefined
constants to give us a little more magic.
0:10
These special constants both start and
end with double underscores, and
0:16
are not case sensitive.
0:20
But, if we start changing
the case all over the place,
0:22
it makes things very difficult to read.
0:26
You should write all
constants in uppercase.
0:29
Magic constants can provide
a lot of information.
0:32
Let's take a look at a few of the most
useful in relation to objects.
0:36
Let's modify the to string method.
0:41
Instead of only displaying
the recipe title,
0:43
let's give it a bit more information
about the object itself.
0:46
The first magic constant
we'll use is class.
0:50
Written __CLASS__.
0:54
This gives us the name
of the class itself.
1:00
Let's use this in a message.
1:02
Let's also remove our call
to the display recipe.
1:37
Now let's run our script.
1:44
Now we see that you are calling a recipe
object with the title, My First Recipe.
1:47
Looks good so far.
1:53
Let's add a little more magic.
1:54
There are two constants that reference
the file where the class is stored.
2:00
The first one is __FILE__.
2:04
This gives us the full
path with the file name.
2:08
We can get just the file name by combining
that with the function, basename.
2:11
We could use the function dir
name with file as well to pull
2:19
only the directory path.
2:23
But we have another constant that
you'll often see the __DIR__ or
2:25
directory constant.
2:29
This constant gives us the full path
to the file without the file name.
2:33
I use __DIR__ often when I
want to link to another file
2:37
within the same directory as this file.
2:41
It works even if this file
is included in another file.
2:43
Let's add both of these
to our message as well.
2:47
Next, let's show specific information
about where in the class we are currently
3:09
looking.
3:13
__LINE__ will tell us the current
line number in the file and
3:15
the __METHOD__ will tell us the name
of the method we are using.
3:19
Let's add these as well.
3:26
Finally, let's show a list of the methods
in case the user is looking for something.
3:50
This is another place where we use
a function in combination with
3:56
a magic constant.
3:59
get_class_methods.
4:00
We then pass it the __CLASS__.
4:04
Now let's add this to our output.
4:08
Let's use the implode.
4:31
When we finish,
we want to return the output.
4:41
Now we're ready to run our script again.
4:45
Wonderful, we have a lot of information
about our object that we can use to update
4:54
the call to our object.
4:59
For example,
I can look through the list of methods and
5:01
see the exact name of my getters and
setters.
5:04
For security purposes,
5:08
you should not display the full
directory path on a production server.
5:09
It can give hackers much more
information than you want.
5:14
Let's add this two string method
to our render class as well.
5:17
Then we'll be able to show
the methods that are available.
5:21
Let's copy this.
5:26
And we'll go in to render.
5:28
We only want to say what
methods are available.
5:33
But let's change this so it tells us
which type of object we're using.
5:38
Now let's go back to our cookbook.
5:53
Since we don't have an object,
we can just echo new Render.
5:57
And now let's run our code.
6:05
Great, we see the results of
the render object as well.
6:10
You need to sign up for Treehouse in order to download course files.
Sign up