html - Why do PHP classes need to be instantiated?
我想大声告诉你
我想大声告诉你 2017-05-24 11:34:11
0
8
854

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

我想大声告诉你
我想大声告诉你

reply all(8)
迷茫

->,插入式解引用操作符(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

$person=new Person();
$person.username="godtoy";

这种方式,每一个都是一个实例

The advantage of using static is that it is more convenient if it is a tool class

Utils::max(val1,val2);
为情所困

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

A class is an abstraction of an object, and an object is an instance of a class, just like human beings are an abstraction of all people. For example, if people want to die, they will die. This is an attribute of humans (classes). Now a child (instantiation) is born , then you will die if you seek death, which is effective for this child (object)

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类,那么这个类是一个图纸,你不能拿一个图纸出去卖吧,所以你需要newProduction of a car before it can be sold and driven. Because you can refer to this drawing to produce many cars.

Of course thisCar可能会有一些静态方法或者属性,这是你不需要生产汽车就可以使用的,比如你要看图纸,那就是Car的静态方法show.

滿天的星座

If you use static methods, there is no such thing as a constructor.

迷茫

You need singleton pattern

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template