Three features of PHP object-oriented operation-encapsulation, inheritance, polymorphism (upper)

Source: Internet
Author: User

<?php
Three major features: encapsulation, inheritance, polymorphism
Encapsulation Practices:
1. Change member variables to private
2. Doing methods in a class indirectly manipulating member variables
3. Control conditions in the method Riga
Encapsulation Purpose: Make classes more secure
/* Class ren{
Public $name;
private $age;//change the variable to private */

The first method of doing
Public function Setage ($a) {//Assignment
if ($a >=18 && $a <=50) {
$this->age = $a;
}else{
echo "The age is not within the range";
}
}
Public Function Getage () {//value
return $this->age;
}


Generic method indirect Operation member variable


Magic method to assign values to variables
Public Function __set ($k, $v) {//Do a function indirect operation
$this $k = $v;
}
The Magic method of assigning variable value
Public Function __get ($k) {
return $this $k;
}
}
$s = new Ren ();//The first object created

Assignment, value output of the first method
$s->setage (18);
Var_dump ($s);
echo $s->getage ();


Assignment, value output (Universal) for the first method
Encapsulation of the first assignment output
/* $s->__set ("age", 20);
Var_dump ($s); */

Encapsulation of the second assignment output
/* $s->age = 20;
Var_dump ($s); */

Value output
/* $s->age = 20;
Echo $s->age; */

Object-oriented instances

Eg: a large circle contains a small circle, a circle radius of 10, a small circle radius of 5, the area of the circle minus the small circle.

Class circle{
Public $r;
Public $area;
Public $cir;
Public Function area () {
return 3.14* $this->r* $this->r;
}
Public Function Cir () {
return 2*3.14* $this->r;
}
Public function __construct ($r) {
$this->r = $r;
}

}
$r 1 = new Circle (10);//Create First Circle
$r 2 = new Circle (5);//Create a second circle
$area = $r 1->area ()-$r 2->area ();
echo "$area <br>";
$cir = $r 1->cir ()-$r 2->cir ();
Echo $cir;

?>

Three features of PHP object-oriented operation-encapsulation, inheritance, polymorphism (upper)

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.