Home >Backend Development >PHP Tutorial >PHP class declaration and PHP class usage examples sharing_PHP tutorial

PHP class declaration and PHP class usage examples sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:06719browse

Copy code The code is as follows:


/**The PHP language supports object-oriented programming. Anyone who has studied Java and C++ knows about object-oriented programming!
*If you are not sure, just go to Baidu and ask.
*/

//Let’s define a class. The keyword for defining a class is "class".
class computer {

/Define an addition
function add ($n1,$n2) {

$sum = $n1 + $n2;
return $sum;
} //Define one Subtraction
function jf ($n1,$n2) {

$sum = $n1 - $n2;
return $sum;
} //Define a multiplication
function cf ($n1,$n2) {

$sum = $n1 * $n2;
return $sum;
}
}

/**
How to use methods in a class is to create an object of the class and use the object to call the method
The following is the method of calling the class
*/

//$com is the object of the computer class. To create an object, use the new keyword
$com = new computer();

//Call the object in the class For methods, use object name-> method name (parameters, parameters...)
echo $com -> add(1,2);
echo $com -> jf(1,2);
echo $com -> cf(1,2);

//If these methods are called by buttons, you can simply write a calculator,
//Each button Corresponds to a method
?>




http://www.bkjia.com/PHPjc/746865.html

www.bkjia.com

http: //www.bkjia.com/PHPjc/746865.htmlTechArticleCopy the code as follows: ?php /**php language supports object-oriented programming. For object-oriented programming , Anyone who has studied Java and C++ knows it! *If you are not sure, go to Baidu and ask...
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