PHP class declaration and PHP class use method sample Share _php instance

Source: Internet
Author: User
Copy Code code as follows:

<?php

/**php language is to support object-oriented programming, for object-oriented programming, Learning Java and C + + people know Ah!
* If you are not clear to Baidu to ask about it.
*/

Let's define a class that defines the class as the key word "class."
Class Computer {

Inside the class you can create methods, variables, etc.

Define an addition
function Add ($n 1, $n 2) {

$sum = $n 1 + $n 2;
return $sum;
}
Define a subtraction
function JF ($n 1, $n 2) {

$sum = $n 1-$n 2;
return $sum;
}
Define a multiplication
Function CF ($n 1, $n 2) {

$sum = $n 1 * $n 2;
return $sum;
}
}

/**
How to use the methods in the class, you create the object of the class, call the method with the object
Here's how to invoke a class
*/

$com is the object of the computer class, creating the object with the New keyword
$com = new computer ();

Calling methods in a class uses the object name-> the method name (parameters, Parameters ...). )
echo $com-> Add (1,2);
echo $com-> JF (1,2);
echo $com-> CF (1,2);

These methods are called by the button, you can simply write a calculator to
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.