I learned the MVC structure today and found that when calling methods in a class in the code, the class must be instantiated first, and finally the method of the class is called
for example:
$xxxxx = new xxxxx();
$xxxxx->ccc('hello wolrd');
But can’t it be called directly like the following?
xxxxx::ccc('hello wolrd');
Why must it be instantiated? What can instantiation bring us? What are the benefits compared to calling it directly? Ask God to clarify my doubts
->
,插入式解引用操作符(infix dereference operator),将对象的引用指向对象,例如对象->对象的方法。::
,作用域解析操作符(Scope Resolution operator),用于调用类的静态成员变量或是类之间的调用。详见博文。
也是说
$xxxxx->ccc('hello wolrd');
为调动类中的普通方法,要先实例化类的实例。xxxxx::ccc('hello wolrd')
To call a static method in a class, it can be called directly without instantiating the class.After instantiating a class, you can access the methods and properties of the class.
Static methods can independently complete a stateless operation.
More related to static methods and instantiated methods.
OOP programming ideas, give an example
The advantage of using static is that it is more convenient if it is a tool class
The poster is very cute and the questions he asked are also very cute. Why would you bother with this kind of question...
Obviously, the following method can also be called, both are possible, but the difference is that only the static member functions of the class can be called in the following method
As for why it needs to be instantiated, I guess the questioner doesn't understand it at all. What is a class?
I’m really not humble, I can’t give too many philosophical and detailed explanations, I can only say
The reason why we want to abstractly summarize the characteristics of human beings is to enable us to better learn how to do it. For example, if you are a doctor, you can better treat each individual (the key is to instantiate Object), of course each individual is different, you can read the documentation and other more detailed answers
Mainly for object-oriented purposes, you can instantiate an object through some parameters. You are making a static call and the object does not exist
Instantiation is like a TV with many programs. Now you want to watch TV. If you want to watch TV, you have to turn on the TV. Then you have to turn on the switch button on the TV. Then you need the new keyword to instantiate this. Object so that we can watch TV programs and operate the functions inside the TV.
Compared with direct calls, instantiation calls can provide access to methods inside the class. Access modifiers:
public (public, default)
protected
private
And direct calling does not require instance change, it is just convenient to use.
Suppose you define a
Car
类,那么这个类是一个图纸,你不能拿一个图纸出去卖吧,所以你需要new
Production of a car before it can be sold and driven. Because you can refer to this drawing to produce many cars.Of course this
Car
可能会有一些静态方法或者属性,这是你不需要生产汽车就可以使用的,比如你要看图纸,那就是Car
的静态方法show
.If you use static methods, there is no such thing as a constructor.
You need singleton pattern