Basic concepts of classes and objects

Source: Internet
Author: User

Class is an abstraction of an object
Object is an instantiation of a class

Class Ren//Create a category tag and capitalize the first letter (industry rules)
{
member variables , multiple common variables can be defined in a class
var $high;
var $name;
// member properties
/*php not in the * *
member methods (functions), depending on the properties of the class, you can define multiple methods
function Eat ()
{echo "Eat by Hand"}
}

Class Instantiation Object
$r =new Ren (); Create a new object that belongs to a class
$r, name= "Zhang San"; Provides a variable value for the object to be used with "-" to define the value of the object that conforms to a variable of that class.
Echo $r->name;

Example: Finding the difference of two concentric circle area

The first type: process-oriented , the method is not easy to maintain, poor scalability, poor reusability, poor flexibility, in a number of cases, the change is extremely troublesome, you need to write all the content again

$r 1 = 10; Assign a circle radius of 10
$r 2 = 5; Assignment Small Circle radius is 5

$DMJ = 3.14* $r 1* $r 1; Calculate the area of large circle with the formula of area
$XMJ = 3.14* $r $r 2; Calculate the area of small circle with the formula of area

$MJ = $DMJ-$XMJ; Calculate area difference with large circle area minus small Circle area

Echo $MJ;

The second type: object-oriented , the method is easy to maintain, strong extensibility, reusability, flexibility, the more the number, the more obvious advantages, modify only the individual variable assignment can be changed

<?php
Class Yuan
{
var $r; Define Variable radius r

function Mianji ()//solution for defining method area
{
return 3.14*$this->r*$this->r; Here is the calculation formula of the area is returned to the method, not the output, if the output, then the method is empty, the subsequent mathematical operations can not be done; $this represents the object (which object is called), not the class
}
}

$dy =new yuan (); Define a new object great circle
$dy, r=10; Its radius is 10
$DMJ = $dy-Mianji (); Calling a method in a class to solve a large circle area

$xy =new yuan (); Defines a new object small circle
$xy, r=5; Its radius is 5
$XMJ = $xy-Mianji (); Calling a method in a class to solve a small circle area

$MJ = $DMJ-$XMJ; The area difference is obtained by using two areas for mathematical calculation.
Echo $MJ;

Basic concepts of classes and objects

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.