Home  >  Article  >  Backend Development  >  Some personal views on Class in PHP_PHP Tutorial

Some personal views on Class in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:08:58806browse

作者:深空 来源:超越PHP

  以我的观点来说说PHP中的Class,用于表达的语言都是非正式的语言,也不能确定是否正确。

建立一个类很简单:

class my_class {}


  类到底干什么呢?很多人都说是什么黑匣子,我在这里称它为一个独立的整体。我们只知道类名,而不知道里面有什么东西。那么,该如何使用这个类呢?
  首先:要知道它里面是否定义了公共的变量--专业术语上称它为“属性”。
  其次:要知道它里面定义了什么函数--专业术语中称它为“方法”。
  我都被这些专业术语搞糊涂了,所以干脆不理它了。

  类中的如何定义公共变量,它有什么作用呢?

  很简单,我们来扩充 my_class 类:

class my_class
{
var $username;
}


  看上面很简单,我们定义了一个公共的变量,只是用 var+空格+普通变量名 构成。它有什么用呢?考虑一下函数中,如果我们要访问函数外的变量,是不是要先 global 一下呢?这个想实现的效果也是如此,它是想让这个类中的所有函数都能访问它,而它区别于函数的一个地方,是类的外部也可以随时访问和控制这个变量,我随后再讲外部如何访问它。还有一个区别,不能用复杂的语句给这个变量赋值(具体的等理解了类以后自己去看规则)。

  给它一个默认值:

class my_class
{
var $username = "深空";
}


  OK,定义了一个公共的变量了,接下来定义一个函数(也就是所谓的“方法”):

class my_class
{
var $username = "深空";

function show_username()
{
}
}


  这个定义函数跟普通的定义函数形式上没什么区别了。简单就好,定义一个打印 $username 的函数:

class my_class
{
var $username = "深空";

function show_username($username)
{
echo $username;
}
}


  到这里可能某些人开始迷糊了,呵呵,最关键的就是这里了,看清楚了。现在有三个 $username 了。到底哪个是哪个啊~~

  函数所带的形参,不用解释了吧?这个函数功能就是打印形参所接收的值,也就是如果:

show_username("猪头深空");


  那么它将打印 “猪头深空” ,就这么简单。

  怎么样访问这个函数?肯定不是我上面说的那样直接 show_username("猪头深空"); 了,别急,类有类的一套。如下:

$Name = new my_class();


  这样就初始化上面的那个 my_class 的类了,并把这个对象赋给变量 $Name ,你可以这样理解,这个变量就代表整个类了,呵呵。

  使用类中的函数:

$Name->show_username("猪头深空");


  晕了,为什么这么复杂?还要箭头?其实很形象的。本来已经把类给了变量 $Name 了是吧?也就是 $Name 代表了这个类,然后用一个箭头指向类中的 show_username 这个函数。就是这么简单,也就是说,这个函数是这个类中的,而不是其他的函数--你就理解为表示一个区别吧,呵呵。

  试试看哦,打印出 “猪头深空” 这四个字了。你说为什么要这么复杂?用函数不是也能实现么?我说,这么简单的你当然看不出好处了,我们继续扩充。

  还有一个疑问是:刚才说的“公共的变量”怎么一点用处都没有呢?为什么这个函数不会自动接收这个公共变量 var $username 中的默认值?也就是如果我使用:

$Name->show_username($username);


  会有什么结果呢?答案是没有任何输出。因为你没有给形参 $username 一个值。

  那么该怎么使用这个公共的变量?我们来修改一下这个类:

class my_class
{
var $username = "深空";

function show_username()
{
echo $this->username;
}
}


  哇靠,不是吧,这回连形参都没有了?还多了一个$this->,晕了不是,呵呵。其实这也是类的一个最大的方便之处。
  $this 的作用:访问一个公共的变量,或者类里面的函数。
  访问?这么专业?其实就是用 $this->username 来代替 var $username 而已拉,$this 用来说明它是公共的、可以访问的、函数外部的东西(比如其他变量或函数)。

试试看:

$Name->show_username();


  看到了吧,终于打印 “深空” 这两个字了,娃哈哈。

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 = "Pig Head Deep Sky";


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();


Haha, finally printed “Pig Head Deep Space”. Not bad, it's very convenient. You can modify the printed value arbitrarily without formal parameters~~.

But just printing a name is too boring. Let’s say something welcome. Let’s extend this class and create a function called Welcome:

class my_class
{
var $username = "deep space";

function show_username()
{
echo $this->username;
}

function Welcome()
{
}
}


Well, what function can be implemented? To make it simple, just have the word "welcome" in front of the name

class my_class
{
var $username = "deep space";

function show_username ()
{
echo $this->username;
}

function Welcome()
{
echo "Welcome";
$this-> ;show_username();
}
}


Did you see $this for the second time? It's a little different from last time. What is $this->show_username(); used for? Points to a function in the class. In fact, it calls the show_username function. Use $this to indicate that this function is in the class and parallel to the Welcome function, rather than elsewhere (such as the Welcome function).

The function implemented by the Welcome function is very simple. First, print two words "Welcome", and then execute the show_username function to print the name.

Let’s try this function:

$Name->Welcome();


See it, print out the four words "Welcome to Deep Space" A word.

But I want to print "Welcome to Pig Head Deep Space", what should I do? I'm convinced, let's give the public variable var $username a value:

$Name->username = "Pig Head Deep Space";


Next, print the welcome message :

$Name->Welcome();


Hehe, finally printed "Welcome to Pig Head Deep Space".

How about it? Do you understand the usage of classes? The advantage is that you can call any function in the class. As long as you point it out with $this, you can change the value of a public variable. You can use this public variable in the function in the class. .........There are so many, and its applications are waiting for you to discover.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314602.htmlTechArticleAuthor: Deep Space Source: Beyond PHP From my point of view, Class in PHP is used for expression The language is informal and it is not certain whether it is correct. Creating a class is very simple: cl...
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