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