What does self mean in php?
To really understand what we're talking about when we talk about Show
Let's start off by talking about what a class and an object is. Classes And Objects, ConceptuallySo, what is a class? A lot of people define it as a blueprint or a template for an object. In fact, you can read more About Classes In PHP Here. And to some extent that's what it really is. Let's look at a class:
As you can tell, there is a property on that class called It's very important to note that the class is a static
structure. Which means that the class An object on the other hand is what's called an instance of a Class. What that means is that we take the "blueprint" of the class, and use it to make a dynamic copy. This copy is now specifically tied to the variable it's stored in. Therefore, any changes to an instance is local to that instance.
We create new instances of a class using the
Therefore, we say that a Class is a global structure, and an Object is a local structure. Don't worry about that funny One other thing we should talk about, is that we can check if an instance is an Defining StateSo let's dig a bit into what a class actually contains. There are 5 types of "things" that a class contains:
So basically, we're storing information on the class and object container using "hints" about static which identify whether the information is shared (and hence static) or not (and hence dynamic). State and MethodsInside of a method, an object's instance is represented by the If a method is called statically, the The interesting thing here is how static calls are made. So let's talk about how we access the state: Accessing StateSo now that we have stored that state, we need to access it. This can get a bit tricky (or way more than a bit), so let's split this into two viewpoints: from outside of an instance/class (say from a normal function call, or from the global scope), and inside of an instance/class (from within a method on the object). From Outside Of An Instance/ClassFrom the outside of an instance/class, our rules are quite simple and predictable. We have two operators, and each tells us immediately if we're dealing with an instance or a class static:
Therefore, From Inside Of An Instance/ClassThings change a bit here. The same operators are used, but their meaning becomes significantly blurred. The object-operator
Calling the So that's how we expect. The meaning of the
Make sense? Didn't think so. It's confusing. Short-Cut KeywordsBecause tying everything together using class names is rather dirty, PHP provides 3 basic "shortcut" keywords to make scope resolving easier.
ExamplesThe easiest way to understand this is to start looking at some examples. Let's pick a class:
Now, we're also looking at inheritance here. Ignore for a moment that this is a bad object model, but let's look at what happens when we play with this:
So the ID counter is shared across both instances and the children (because we're
using
Note that we're executing the Word Of Caution #1Note that the calling context is what determines if an instance is used. Therefore:
Is not always true.
Now it is
really weird here. We're calling a different class, but the This can cause all sorts of bugs and conceptual WTF-ery. So I'd highly suggest avoiding the Word Of Caution #2Note that static methods and properties are shared by everyone. That makes them basically global variables. With all the same problems that come with globals. So I would be really hesitant to store information in static methods/properties unless you're comfortable with it being truly global. Word Of Caution #3In general you'll want to use what's known as Late-Static-Binding by using TL/DRToo bad, go back and read it. It may be too long, but it's that long because this is a complex topic TL/DR #2Ok, fine. In short, TL/DR #3If the object-operator is used ( Is self the same as this PHP?The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.
What is difference between self and static in PHP?PHP new self vs new static: Now that we changed the code in our example to use static instead of self, you can see the difference is that self references the current class, whereas the static keyword allows the function to bind to the calling class at runtime.
Why we use $this in PHP?$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.
How use $this in static method in PHP?In the static method,properties are for the class, not the object. This is why access to static methods or features is possible without creating an object. $this refers to an object made of a class, but $self only refers to the same class.
|