Home >Backend Development >PHP Tutorial >An introductory Class article_PHP tutorial
I just took a quick look at the article about Class that was updated on the homepage (referring to the PHPE article http://www.phpe.net/articles/389.shtml). It’s very good. I recommend reading it.
Exploring classes~~ It took me half a year to roughly understand the functions and implementation of classes. The main reason is that there is no article that I can understand (I have never been exposed to any OO stuff before).
From my point of view, the language used to express Class in PHP is informal, and I am not sure whether it is correct.
Creating a class is easy.
PHP code:-------------------------------------------------- ------------------------------------
class my_class {}
---- -------------------------------------------------- --------------------------
What exactly do classes do? What many people are is a black box, I call it an independent whole here. We only know the class name, but not what is inside. So, how to use this class?
First of all: you need to know whether there are public variables defined in it - called "properties" in professional terms.
Secondly: You need to know what function is defined in it - it is called a "method" in professional terms.
I was confused by all the jargon, so I just ignored it.
How to define public variables in a class and what does it do?
It’s very simple, let’s extend the my_class class
PHP code:--------------------------------- --------------------------------------------------
class my_class
{
var $username;
}
-------------------------------- --------------------------------------------------
Looking at the above, it is very simple. We have defined a public variable, which is just composed of var + space + ordinary variable name. What is it used for? Consider a function. If we want to access variables outside the function, do we need to make it global first? The same is true for this effect. It wants all functions in this class to be able to access it, and one thing that distinguishes it from functions is that this variable can also be accessed from outside the class. I will talk about how to access it from the outside later. . There is another difference. You cannot use complex statements to assign a value to this variable (see the rules for yourself after you understand the class). Give it a default value
PHP code:--------------------------------------------- ----------------------------------------
class my_class
{
var $username = "Deep Space";
}
---------------------------------- --------------------------------------------------
OK, a public variable has been defined. Next, define a function (the so-called "method").
PHP code:-------------------------------------------------- ------------------------------------