Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:19:27+00:00 2026-06-08T10:19:27+00:00

Use $this to refer to the current object. Use self to refer to the

  • 0

Use $this to refer to the current object. Use self to refer to the
current class. In other words, use $this->member for non-static
members, use self::$member for static members.

http://board.phpbuilder.com/showthread.php?10354489-RESOLVED-php-class-quot-this-quot-or-quot-self-quot-keyword

class Demo
{
    private static $name;
    private $age;

    public function __construct($name, $age) 
    {
        self::$name = $name;
        $this->age=$age;
    }   

    public function show()
    {       
        echo "Name : ", self::$name, "<br/>";   //Accessing by self
        echo "Name : ", Demo::$name, "<br/>";   //Accessing by class name 
        echo "Age  : ", $this->age, "<br/>";    
    } 
}

$demo = new Demo("Tiny", 13);
$demo->show();

This produces the following output.

Name : Tiny
Name : Tiny
Age : 13

What is the difference between self::$name and Demo::$name in the preceding snippet?


class Person1
{
    private $name;
    private $address;

    public function __construct($name,$address)
    {
        $this->name = $name;
        $this->address = $address;
    }

    public function show()
    {
        echo "Name    : ", $this->name, "<br/>";    
        echo "Address : ", $this->address, "<br/>";  //Accessing by this
    }
}

$p1=new Person1("xxx", "yyy");
$p1->show();

class Person2
{
    private $name;
    private $address;

    public function __construct($name,$address)
    {
        self::$name = $name;
        self::$address = $address;
    }

    public function show()
    {
        echo "Name    : ", self::$name, "<br/>";    
        echo "Address : ", self::$address, "<br/>"; //Accessing by self 
    }
}

$p2=new Person1("xxx", "yyy");
$p2->show();

Preceding two classes Person1 and Person2 produce the same output as follows.

Name : xxx
Address : yyy

What is the difference between (as in the Preson1 class, the show() method)

$this->name;
$this->address;

and (as in the Preson2 class, the show() method)

self::$name;
self::$address;

in this context?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:19:28+00:00Added an answer on June 8, 2026 at 10:19 am

    Using Demo::$name and self::$name from within the Demo class is, in effect, the same. You can access it by name due to the fact that the the variable is static and accessible, just like you’d be able to access a public static variable from a different class using the classes’ name, for example.

    So in that case, accessing by name is mostly just a by-product of being able to access any public static variable from an accessible class.

    The second output produces the same as the first because you’re apparently creating a new Person1 object, not a Person2 object (see: $p2=new Person1("xxx", "yyy");)

    If you were to create a Person2 object, then you’d get an error, because you can’t assign values to a static variable that hasn’t been declared (you’ve got object-level variables in name and address, but not static). If you declared them as static, then you’d get the same output from the modified show method due to the fact that it’s using a static call.

    If you’re not sure what the difference between object (read: instance) variables and static variables are, then i encourage you to partake in some Googling, but basically static variables exist at the class level, not the object level, and are accessible from any class method or object of the specified type (so if you had 100 instances of Person2 they’d all have access to the same name variable, for example), whereas instance variables are unique to the individual object itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In J2ME to create the Display class object we use private Display d; d=Display.getDisplay(this);
Possible Duplicate: “Invalid use of 'this' in non-member function” in objective-c context? If I
i use this line of code : [self parseXMLFileAtURL:url]; And i get this error
I use this class to parse a .css file. https://github.com/sabberworm/PHP-CSS-Parser I thought it's easy
this is the url i refer to: http://support.microsoft.com/kb/317559 i study their method and had
use this website a lot but first time posting. My program creates a number
I use this function for transforming URLs to images on output. function InsertLink(T){ var
I use this script in two different servers: function curlGetFileInfo($url, $cookies=default){ global $_config; $ch
I use this code : MethodInvoker TileLoadCompleteMethodInvoker = delegate() { CleanStatusLabelTimer.Enabled = true; MemoryTextBox.Text
i use this script to load content of some div, inside another div: $(function()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.