PHP Class Tutorial – Part 2 – What is $this->
Please make sure you’ve followed my first PHP Class Tutorial before starting this one, as it follows on using the previous example. This tutorial will explain what $this-> is all about, and how to further your PHP class knowledge!
PHP Class Tutorial Chapters
Part 1 – Jumping In With Two Feet
Part 2 – What does $this-> mean in a PHP class file?
Part 3 – What Are Class Constructors?
I’m ready to go, set me up!
Our first example used the following – very simple – class file:
class Time {
function GenerateCurrentTime(){
$sTime = gmdate("d-m-Y H:i:s");
return $sTime;
}
}
Now, let’s add in the ability for our class file to look X number of days into the future shall we? Add in a new function to the class file that looks like:
function ShowFutureDate($iAddDays=0){
$sTime = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));
return $sTime;
}
Now, if we were to run
ShowFutureDate(5); print 'The time in 5 days is: ' . $sTime; ?>
We’d see the date in 5 days time. But for the purposes of this tutorial, let’s change things slightly so that we can use $this-> and start to understand it!
Firstly, let’s declare a variable that we can use within any of the class functions. We accomplish this in the following way:
class Time {
var $sTime;
function GenerateCurrentTime(){
$sTime = gmdate("d-m-Y H:i:s");
return $sTime;
}
function ShowFutureDate($iAddDays=0){
$sTime = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));
return $sTime;
}
}
Notice how we added ‘var $sTime;’ at the beginning of the class file? Do the same in your code.
Let’s start using this as a variable, available only to the scope of the class file. It’s good practice to declare your class variables (only accessible within the scope of the class file / functions) in the header. Why are you calling that bit of space between the opening curly brace and the first function the header I hear you all ask? It’s just what I call it, as do many other developers. It’s a nice bit of white-space where you can declare your class variables within the PHP class.
Ok done that, so what is $this->?
Right, we’re going to remove the references to $sTime in both of our functions we now have, and we’re going to replace it with $this->sTime. Our entire class file should now look like the following:
class Time {
var $sTime;
function GenerateCurrentTime(){
$this->sTime = gmdate("d-m-Y H:i:s");
return $this->sTime;
}
function ShowFutureDate($iAddDays=0){
$this->sTime = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));
return $this->sTime;
}
}
If you run the code, and execute the functions on a page where you have this class file included (not within this class file!) – you’ll see that we’re still returning the same shizzle as before. That’s fine. Good infact – it means you’re following along well! Let’s say that you now run the function:
$oTime->ShowFutureDate(5);
We know that the class, $oTime has now set it’s class variable – $sTime – to the current date + 5 days. We can now use that variable ANYWHERE in our page without having to run that function again, and again. THAT my friends is the beauty of class variables. As long as you have executed that function, we can now use:
echo $oTime->sTime;
Anywhere, and as many times in the page as we like. It makes for much faster code! If you think of $this-> outside of the class as the actual class name – $oTime, then you will have no problem using $this-> within your class files, to write much much more efficient code.
Final PHP Class File Code
I’s worth noting in this PHP class file tutorial, that we should really remove the return references in our code, as we don’t really need them anymore. Our final code should now look like this:
class Time {
var $sTime;
function GenerateCurrentTime(){
$this->sTime = gmdate("d-m-Y H:i:s");
}
function ShowFutureDate($iAddDays=0){
$this->sTime = gmdate("d-m-Y H:i:s", strtotime("+" . $iAddDays . " days"));
}
}
The next tutorial will talk about Class Constructors!
PHP Class Tutorial Chapters
Part 1 – Jumping In With Two Feet
Part 2 – What does $this-> mean in a PHP class file?
Part 3 – What Are Class Constructors?
Thankyou
[...] This tutorial will explain what $this-> is all about, and how to further your PHP class knowledge! View Tutorial $this->, PHP Class, PHP [...]
hey nice tutorial , I would like to know about Class Constructors more, thanks keep it up, your helping us to understand simply the php class. thanks again.
[...] PHP Class Tutorial – Part 2 – What is $this-> | edrackham says: September 16, 2010 at 8:29 pm [...]
Pogi – Your wish is my command!
http://edrackham.com/php/php-class-tutorial-part-3-what-are-class-constructors/
[...] PHP Class Tutorial – Part 2 – What is $this-> | edrackham says: September 28, 2010 at 8:44 pm [...]
Great Tutorial, very simple to understand
Thank you
This tutorial was easy to understand, minus the part about executing functions. I am assuming (though it isn’t very clear) that the functions you want to execute (say $oTime->ShowFutureDate(5); ) go into a separate file, not the class file? If you could specify throughout your tutorial, it would help immensely. Keep up the fantastic work! Thanks for what you do.
Hi Erin,
Thank you for your comments! I have updated the tutorial slightly to (hopefully) make this a little clearer. You *shouldnt* be running class functions using:
$CLASSNAME->CLASSMETHOD()
From within the class. This should be done outside the class file. The:
$this->CLASSMETHOD()
Must only be used from within the class file itself.
I hope this helps make sense!
Thanks for your comments
Ed.
Ed,
Thanks so much for the clarification, and the new wording on your tutorial makes this much clearer. Thanks for helping out, your work is appreciated!!
“We can now use that variable ANYWHERE in our page without having to run that function again”
So you first had:
$sTime = $c_time->GenerateCurrentTime();
Then declared $sTime in the class:
$oTime->ShowFutureDate(5);
echo $oTime->sTime;
What I’m confused is, what difference is there? The variable $sTime is available after the function is called, in either method. Why is the latter “faster”? It just seems like more code to type for no added benefit.
Well done…
Quick question…
Both functions within the class point to $sTime.
How does ” 1. echo $oTime->sTime; ” work if both functions have been previously called, and how do you know if you’re receiving the correct value?
Thanks
This is nice but,
What was the advantage in using $this-> ?
and how can I sort this portion? when I have an output function?
Thnks
Thanks a lot Ed, this really helped me out to understand classes usage!
asdasdasda asdas asdas asas gegerreg erg er
I can’t tell you how much I appreciate this tutorial! It’s been a great help.
I believe I now understand why using $this-> can steamline things but I’m having a slight problem. You said that after using $this->, it’s safe to remove the return statements. The problem is that removing the return statements causes my output to be blank:
The time is:
The time in 5 days is:
It works just fine with if the return statements are there following your instructions to a T. Any idea why this would be?