To understand the previous article, read this one first...
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 very simple:
class my_class {}
What exactly does a class do? Many people say it is a black box, but 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 these technical terms, so I simply 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:
class my_class
{
var $username;
}
The above is very simple, we defined a public variable, just use var+space+ordinary variable Name composition. 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, which is to allow all functions in this class to access it, and one thing that distinguishes it from functions is that the outside of the class can also access and control this variable at any time. I will talk about the outside later. How to access it. 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:
class my_class
{
var $username = "深空";
}
OK, a public variable is defined, and then a function is defined (also known as a "method" ):
class my_class
{
var $username = "深空";
function show_username()
{
}
}
This definition function is no different in form from an ordinary definition function. Just keep it simple, define a function that prints $username:
class my_class
{
var $username = "深空";
function show_username($username)
{
echo $username;
}
}
At this point some people may be confused, haha, the most important thing is here, see clearly. There are now three $usernames. Which one is which~~
There is no need to explain the formal parameters of the function, right? The function of this function is to print the value received by the formal parameter, that is, if:
show_username("猪头深空");
Then it will print "Pig Head Deep Sky", it's that simple.
How to access this function? It's definitely not the direct show_username("Pig Head Deep Space"); as I said above. Don't worry, there are different classes. As follows:
$Name = new my_class();
In this way, the my_class class above is initialized, and this object is assigned to the variable $Name. You can understand it this way, this variable represents the entire class. ,hehe.
Use functions in classes:
$Name->show_username("猪头深空");
I’m confused, why is it so complicated? Want an arrow? It's actually very vivid. You have already given the class to the variable $Name, right? That is, $Name represents the class, and then an arrow points to the show_username function in the class. It's that simple, that is to say, this function is in this class, not other functions - you can understand it as indicating a difference, haha.
Try it and print out the four words "Pig Head Deep Sky". Why do you think it's so complicated? Isn’t it also possible to use functions? I said, of course you can’t see the benefits of such a simple thing, let’s continue to expand.
Another question is: Why are the "public variables" mentioned just now useless? Why doesn't this function automatically receive the default value in this public variable var $username? That is, if I use:
$Name->show_username($username);
What will be the result? The answer is no output. Because you didn't give the formal parameter $username a value.
So how to use this public variable? Let’s modify this class:
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
}
Wow, isn’t it? There are no formal parameters this time? There is also an extra $this->, which makes me dizzy, haha. In fact, this is also one of the biggest conveniences of classes.
The role of $this: access a public variable or a function in a class.
Visit? So professional? In fact, $this->username is used instead of var $username. $this is used to indicate that it is public, accessible, and something outside the function (such as other variables or functions).
Try it:
$Name->show_username();
You see, the words "Deep Space" are finally printed, Wahaha.
I don’t want to print the words “Deep Space”. I want to print “Pig Head Deep Space”. What should I do? It's very simple, we reassign this public variable. I'm impressed with you.
$Name->username = "猪头深空";
Do you understand the meaning of this? $Name->username represents this public variable in the class. I don’t need to explain the equal sign assignment.
Let’s print it again:
$Name->show_username();
哈哈,终于打印“猪头深空”了。不错吧,很方便吧,不用形参也能任意修改打印值哦~~。
不过单单打印一个名称也太没意思了,我们说点欢迎的话吧,来扩充一下这个类,创建一个名叫 Welcome 的函数:
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
function Welcome()
{
}
}
恩,实现什么功能好呢?简单点吧,就实现在名字前面有 “欢迎” 两个字好了
class my_class
{
var $username = "深空";
function show_username()
{
echo $this->username;
}
function Welcome()
{
echo "欢迎";
$this->show_username();
}
}
第二次看到 $this 了吧?和上次有点不同,$this->show_username(); 干什么用呢?指向类中的一个函数,其实它就是调用 show_username 这个函数,用 $this 来表示这个函数在类中并且和 Welcome 函数平行,而不是在其他地方(比如Welcome函数中)。
Welcome 函数实现的功能很简单,首先打印两个字"欢迎",然后接下去执行 show_username 函数,打印名字。
来试试这个函数吧:
$Name->Welcome();
看到了吧,打印出“欢迎深空”这四个字了。
可是我要打印“欢迎猪头深空”,怎么办?我服了你了,我们给公共变量 var $username 一个值吧:
$Name->username = "猪头深空";
接下去打印欢迎语:
$Name->Welcome();
嘿嘿,终于打印“欢迎猪头深空”了。
怎么样?明白了类的用法了么?好处在于能够调用类中的任意函数,只要用 $this 指出来,可以改变一个公共变量的值,可以在类中的函数中使用这个公共变量。………多了去了,它的应用等待你去发现了。
http://www.bkjia.com/PHPjc/508512.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508512.htmlTechArticle要看懂前一篇,先把這篇看看先...... 对类的摸索~~俺用了半年时间才大概理解类的作用和实现。主要是没有一篇能让我理解的文章(之前...