I hava script with both $this keyword and without it. Each one does the same thing.
I cant understand what does $this do.
this with $this

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;
  }
}

and this is without $this version

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;
  }
}

Thanks in advance for attention

Recommended Answers

All 5 Replies

Use $this if you want to update the value of that particular property of the object (if you want to save the value). If you want to use $sTime just in that function to compute a time, return it, and forget about it, then don't use $this.

mypage.php

require('time.class.php'); // replace this with the filename of your class

$mytime = new Time();
echo '<div>Before time: ' . $mytime->sTime . '</div>'; // should be empty or null

$mytime->GenerateCurrentTime();

/* If your function says $this->sTime, the time will show up here. If your function instead uses $sTime (no $this), the time will again be empty or null here. */
echo '<div>After time: ' . $mytime->sTime . '</div>';

thank you very much. Is it true when we use with this it is not necessary to use return statement?

It entirely depends on what your function is doing. For example, if I'm writing a function to set the value for $firstname in an object person, then there's probably no need to return anything. On the other hand, if I were writing a function to retrieve the value of $firstname, then I would absolutely need a return statement. See the following example class Person:

class person
{
  var $firstname;
  var $lastname;

  function __construct()
  {
    $this->firstname = '';
    $this->lastname  = '';
  }

  function setFirstname(firstname)
  {
    // since I'm just setting the value right now and not using it
    // there's no need to return anything.
    $this->firstname = firstname;
  }

  function getFirstname()
  {
    return $this->firstname;
  }
}

I agree it entirely depends on what the function is doing. However I disagree with the code example. If you're going to write classes you should be using PHP 5 syntax.

  • No reason to have a constructor as the variables default to 'NULL' you could also default them in the class.
  • Variables and functions should have scope, public/protected/private etc.
  • If you're not going to return anything from your function returning $this will allow you to chain your method calls together. See Below
class person
{
  public $firstname = '';
  public $lastname = '';

  public function setFirstname( $firstname )
  {
    $this->firstname = $firstname;
    return $this;
  }

  public function getFirstname()
  {
    return $this->firstname;
  }
}

By returning $this from functions that don't have a return value, usually setters, you can chain the method calls together.

<?php
$person = new person();
echo $person->setFirstname('John')->getFirstname(); //John
echo $person->setFirstname('John')->setFirstname('Joe')->setFirstname('Bill')->getFirstname(); //Bill

This example is not a very good example of method chaining but take something like the following example:

<?php

class Person
{
  public $firstName;
  public $lastName;
  public $birthday;

  public function setFirstname( $name ){ ... }
  public function setLastname( $name ){ ... }
  public function setBirthday( $bday ){ ... }
}

Each setter function would return $this. By using method chaining we can keep calling methods on the return of each function as each function will always return a reference to itself.

e.g.

<?php
$person = new Person();
$person->setFirstname('John')
       ->setLastname('Doe')
       ->setBirthday('01/01/2011');

There is no benefit to this over just calling $person->method(); in multiple statements, but this is something I see that few people know about.

i have read from internet that here is not necessary
return like below.

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"));
  }
}

Is it true. or somebody has written this false
thank you for attention

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.