Home php教程 php手册 php面向对象之class

php面向对象之class

Jun 13, 2016 am 10:39 AM
class php under right of language Talk about it For

下面来说说 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();

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

  我不打印“深空”这两个字,我要打印“猪头深空”,怎么办?很简单,我们给这个公共变量重新赋值拉。服了你了。

$Name->username = "猪头深空";

  这个能明白意思么?$Name->username 表示的是类里面的这个公共变量。等号赋值不用我解释了。

  我们再来打印看看:

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

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles