Home  >  Article  >  Backend Development  >  Teach you how to use a PHP class to implement addition, subtraction, multiplication and division between two numbers

Teach you how to use a PHP class to implement addition, subtraction, multiplication and division between two numbers

藏色散人
藏色散人Original
2021-07-30 17:50:475404browse

Before starting this article, I would like to ask you how much you know about PHP object-oriented? Object-oriented is referred to as OOP, and I guess everyone is familiar with it. Object-oriented includes classes, objects, member variables, member functions, inheritance, parent classes, subclasses, polymorphism, overloading, abstraction, encapsulation, constructors, and destructors. The concept of functions cannot be fully introduced in one article. You can study the course "PHP Object-Oriented Programming (Jade Girl Heart Sutra Edition)".

So the focus of this article is how to create a PHP class to implement addition, subtraction, multiplication and division between two numbers. This is similar to a very simple calculator function, which is quite interesting~

The implementation is Very simple, continue reading:

First open the PHP editor and create a PHP sample file;

The above code:

<?php
class MyCalculator {
    private $_fval, $_sval;
    public function __construct( $fval, $sval ) {
        $this->_fval = $fval;
        $this->_sval = $sval;
    }
    public function add() {
        return $this->_fval + $this->_sval;
    }
    public function subtract() {
        return $this->_fval - $this->_sval;
    }
    public function multiply() {
        return $this->_fval * $this->_sval;
    }
    public function divide() {
        return $this->_fval / $this->_sval;
    }
}
$mycalc = new MyCalculator(12, 6);
echo "相加等于".$mycalc-> add()."<br>"; // 显示18
echo "相乘等于".$mycalc-> multiply()."<br>"; // 显示72
echo "相减等于".$mycalc-> subtract()."<br>"; // 显示6
echo "相除等于".$mycalc-> divide()."<br>"; // 显示 2

Output the calculation results:

Teach you how to use a PHP class to implement addition, subtraction, multiplication and division between two numbers

Here is a brief introduction to the constructor and the definitions of private and public:

The constructor is a special method, mainly used to Initialize the object when creating the object, that is, assign initial values ​​to the object member variables, and always use it with the new operator in the statement that creates the object.

private means private. This keyword represents a private class member, which can only be accessed by the class in which it is defined. As in the above example, we declared a private method.

Public means public: public class members can be accessed anywhere.

Finally, I would like to recommend to you the latest free course on our platform "Entering the World of PHP from 0"~ Come and learn!

The above is the detailed content of Teach you how to use a PHP class to implement addition, subtraction, multiplication and division between two numbers. For more information, please follow other related articles on the PHP Chinese website!

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