Php class declaration and php class usage examples

Source: Internet
Author: User

Copy codeThe Code is as follows:
<? Php

/** The php language supports object-oriented programming. Anyone who has learned java and C ++ knows about object-oriented programming!
* If you do not know, just ask baidu.
*/

// Let's define a class. The key word of the defined class is "class"
Class computer {

// You can create methods and variables in the class.

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

$ Sum = $ n1 + $ n2;
Return $ sum;
}
// Define a subtraction
Function jf ($ n1, $ n2 ){

$ Sum = $ n1-$ n2;
Return $ sum;
}
// Define a multiplication
Function cf ($ n1, $ n2 ){

$ Sum = $ n1 * $ n2;
Return $ sum;
}
}

/**
To use methods in a class, you must create class objects and call methods with objects.
The following describes how to call a class.
*/

// $ Com is the computer class object. The new keyword is used to create the object.
$ Com = new computer ();

// Call the method in the class with the Object Name> method name (parameter, parameter ...)
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 compile a calculator,
// Each button corresponds to a method
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.