Home Backend Development PHP Tutorial 基于PHP 面向对象之成员方法详解_php实例

基于PHP 面向对象之成员方法详解_php实例

Jun 07, 2016 pm 05:24 PM
php object-oriented

用一个列子来读解成员方法:大家可以自己动手写一写,加深理解。

这样一个需求; 希望人可以说话, 做算术题.......,这样就需要使用到成员方法:
1、添加speak 成员方法,输出 我是小明
2、添加jisuan 成员方法,可以计算从 1+..+1000的结果
3、修改jisuan 成员方法,该方法可以接收一个数n,计算 1+..+n 的结果
4、添加add 成员方法,可以计算两个数的和

参考代码:

复制代码 代码如下:

    class Person{

        public $name;
        public $age;

        //添加成员方法
        public function speak(){

            echo "我是小明";
        }

        public function jisuan(){
            //计算从 1+..+1000的结果
            $result =0;
            for($i=1;$i                $result+=$i;
            }
            //把计算结果ruturn 返回
            return $result;
        }

        public function jisuan2($n){
            //计算从 1+..+n的结果
            $result=0;
            for($i=1;$i                $result+=$i;
            }
            //return
            return $result;
        }

        //计算2个数的和
        public function add($num1,$num2){
            return $num1+$num2;
        }
    }

    $person1=new Person;
    //人说话
    $person1->speak().'
';
    //计算
    echo '
'.$person1->jisuan();
    //echo '
'.$person1->jisuan(100);
    //带参数的
    echo '计算的结果是:'.$person1->jisuan2(5);
    //计算2个数的和
    echo "
50+50=".$person1->add(50,51);
?>


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

An in-depth explanation of PHP's object-oriented encapsulation An in-depth explanation of PHP's object-oriented encapsulation Aug 11, 2023 am 11:00 AM

In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

How to achieve seamless switching and replacement of objects through PHP object-oriented simple factory pattern How to achieve seamless switching and replacement of objects through PHP object-oriented simple factory pattern Sep 06, 2023 am 08:01 AM

How to achieve seamless switching and replacement of objects through PHP's object-oriented simple factory pattern Introduction: In PHP development, object-oriented programming (Object-oriented Programming, referred to as OOP) is a very common programming paradigm. The object-oriented design pattern can further improve the maintainability and scalability of the code. This article will focus on the simple factory pattern in PHP to achieve seamless switching and replacement of objects. What is the simple factory pattern? Simple factory pattern (Simple

How to create flexible object instances using PHP object-oriented simple factory pattern How to create flexible object instances using PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:12 PM

How to create flexible object instances using the PHP object-oriented simple factory pattern. The simple factory pattern is a common design pattern that creates object instances without exposing object creation logic. This mode can improve the flexibility and maintainability of the code, and is especially suitable for scenarios where different objects need to be dynamically created based on input conditions. In PHP, we can use the characteristics of object-oriented programming to implement the simple factory pattern. Let's look at an example below. Suppose we need to create a graphing calculator that can

Understand the object-oriented inheritance mechanism of PHP Understand the object-oriented inheritance mechanism of PHP Aug 10, 2023 am 10:40 AM

Understanding the Object-Oriented Inheritance Mechanism of PHP Inheritance is an important concept in object-oriented programming, which allows the creation of new classes that include the features and functionality of the old classes. In PHP, inheritance can be achieved through the keyword extends. Through inheritance, subclasses can inherit the properties and methods of the parent class, and can add new properties and methods, or override inherited methods. Let us understand the object-oriented inheritance mechanism of PHP through an example. classAnimal{public$name

How to master object-oriented programming skills in PHP development How to master object-oriented programming skills in PHP development Jun 25, 2023 am 08:05 AM

With the development of the Internet, PHP has gradually become one of the most popular programming languages ​​​​in web development. However, with the rapid development of PHP, object-oriented programming has become one of the necessary skills in PHP development. In this article, we will discuss how to master object-oriented programming skills in PHP development. Understanding the Concepts of Object-Oriented Programming Object-oriented programming is a programming paradigm that organizes code and data through the use of objects (classes, properties, and methods). In object-oriented programming, code is organized into reusable modules, thereby improving the program's

How PHP implements object-oriented programming and improves code readability and maintainability How PHP implements object-oriented programming and improves code readability and maintainability Jun 27, 2023 pm 12:28 PM

With the continuous development of Internet technology, PHP has become one of our common website development languages, and PHP object-oriented programming has also become a knowledge point that must be learned. Object-oriented programming (OOP) is a programming paradigm whose core concept is to combine data and behavior into an object to improve code reusability, readability, and maintainability. This article will explore how to use PHP to implement object-oriented programming and improve the readability and maintainability of your code. Basic Concepts of Object-Oriented Programming In object-oriented programming, each object has a set of properties.

Detailed explanation of common problems in PHP object-oriented programming Detailed explanation of common problems in PHP object-oriented programming Jun 09, 2023 am 09:27 AM

PHP language has become a very popular web development language because it is easy to learn and use. Object-oriented programming is one of the most important programming paradigms in the PHP language. However, object-oriented programming is not something that is easy to master, so some common problems often arise. In this article, we will provide a detailed analysis of common problems with object-oriented programming in PHP. Question 1: How to create an object? In PHP, you can use the new keyword to create an object. For example: classMyClass{/

See all articles