search
HomeBackend DevelopmentPHP TutorialSome personal opinions on Class in PHP_PHP Tutorial

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 指出来,可以改变一个公共变量的值,可以在类中的函数中使用这个公共变量。………多了去了,它的应用等待你去发现了。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508512.htmlTechArticle要看懂前一篇,先把這篇看看先...... 对类的摸索~~俺用了半年时间才大概理解类的作用和实现。主要是没有一篇能让我理解的文章(之前...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.