PHP Object-oriented

Source: Internet
Author: User
Tags modifiers

Process oriented
/* $r = 5;
$m = 3.14* $r * $R;

Classes and objects
Object: Anything can be called an object, something instantiated by a class
Class: Something abstracted from all the same objects

Info:code,name,sex,nation,birthday
Object: A specific information p001 Zhang San male han 1990-2-3

To define a class:
Class Ren
{
Public $Name; Member variables
protected $age;
Private $height;
var $sex; Generally do not assign a value to a member variable

function __construct ($s)//constructor
{
$this->sex = $s;
}

function Run ()//member functions (method)
{
echo "This man is running!" ";
}
function Say ()
{
echo $this->name. " I'm talking! ";
}
}

Using classes
1. Instantiating an Object
$r = new Ren ("male");
2. Assigning values to member variables (calling member variables)
$r->name = ' Zhang San '; Assign a value to the name of the object
Echo $r->age;

Var_dump ($R);
3. Calling member Methods
$r->say (); Execute Member method

Access modifiers
1. If you want to add access modifiers, you need to remove Var
2. Three kinds: public protected protected private private
3. If you do not add an access modifier, the default is public

$this references
$this represents the object (which object is called) and does not represent the class

constructor function
1. Special wording: __construct ()
2. Special execution: The first thing to do when creating objects
Action: Initialize an Object


Class Ren
{
Private $name;
Private $sex;
Private $age;

function __construct ($s)
{
$this->sex = $s;
}
Public Function Say ()
{
echo "Hello";
}
function __destruct ()//destructor, object destruction before execution
{
echo "The object was destroyed!" ";
}

/*function setage ($a)//function assigned to age
{
if ($a >10 && $a <50)
{
$this->age = $a;
}
}
function getage ()//Take the value of age
{
return $this->age;
}*/

function __set ($name, $value)//Magic method of assigning a value to a private member inside a class
{
if ($name = = "Age")
{
if ($value >20 && $value <50)
{
$this $name = $value;
}
}
Else
{
$this $name = $value;
}
}

function __get ($name)//Magic method for taking a value on a private member inside a class
{
return $this $name;
}
}

$r = new Ren ("male");
$r->setage (40);
$r->say ();
Echo $r->sex;

$r->__set ("name", "Zhang San");
$r->name = "John Doe"; Executing this statement will automatically call the __set method
$r->sex = "female";
$r->age = 40;

echo $r->getage ();

Var_dump ($R);

Object-oriented three major features
Packaging
Purpose: To make the class more secure and not allow the outside world to directly access the member variables inside the class
Practice: 1. Make the member variables private
2. Do a method to implement the value of the variable or assignment, in the method to add a restriction condition

Using the Magic method provided in the class can also implement the operation of the private member
__set () Feature: will automatically execute, the variable name in the assignment statement as the first parameter, the value of the variable as a second parameter call __set method
__get () Feature: will automatically execute, the variable name in the value statement as a parameter call __get method


Inherited
Polymorphic

PHP Object-oriented

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.