PHP Object-oriented

Source: Internet
Author: User

PHP Object-oriented:

First, how to define the class

declaring classes

Class Dog

{

var $name;

var $age;

var $pinzhong;


function Jiao ()

{

echo "{$this->name is called}"; #this代表当前对象

}

}

Ii. Instantiation of objects

$dog = new Dog ();

Calling member variables

$dog->name = "Wang Choi";

$dog->age = 10;

$dog->pinzhong = "Husky";

Calling methods

$dog->jiao ();

Third, this keyword

$this->name;this represents the object, if you want to call member variables in member methods, you must use $this to refer to

Four, the constructor function

Class Dog

{

var $name;

var $age;

var $pinzhong;

function __construct ($n, $a, $p) #下划线有两个

{

$this->name = $n;

$this->age = $a;

$this->pinzhong = $p;

}


function Jiao ()

{

echo "{$this->name is called}"; #this代表当前对象

}

}

$dog = new Dog ("Wang Choi", 10, "Jin Mao");


$dog->jiao ();

Five, the destruction function



Class Dog

{

var $name;

var $age;

var $pinzhong;

function __construct ($n, $a, $p) #下划线有两个

{

$this->name = $n;

$this->age = $a;

$this->pinzhong = $p;

}


function Jiao ()

{

echo "{$this->name is called}"; #this代表当前对象

}

function __destruct () #析构函数

{

echo "Goodbye";

}

}

$dog = new Dog ("Wang Choi", 10, "Jin Mao");


$dog->jiao ();

Six, Package:

The member variable inside the descending class and a private modifier are changed to be proprietary, in order to protect the members of the class from being arbitrarily accessed by the outside world, thus ensuring the security of the class. If the variable is set to private, how to access:

1, write your own function Get function set function, and then to the variable assignment and value

2, using the system's own __get () function and the __set () function to implement the variable assignment and value inside the class

Class Dog

{

Private $name;

Private $age;

Private $pinzhong;

/*function SetName ($n)

{

$this->name = $n;

}


function Setage ($a)

{

if ($a >=1 && $a <=5)

{

$this->name = $a;

}

}*/

function __set ($name, $value)

{

$this $name = $value;

}

function __get ($name)

{

return $this $name;

}

The function __construct ($n, $a, $p) #下划线有两个 The constructor of the class, referencing it with new.

{

$this->name = $n;

$this->age = $a;

$this->pinzhong = $p;

}


function Jiao ()

{

echo "{$this->name is called}, it {this->age} years old"; #this代表当前对象

}

function __destruct () #析构函数

{

echo "Goodbye";

}

}

$dog = new Dog ("Wang Choi", 10, "Jin Mao");

$dog->name = "Xiao Qiang";

$dog->age = "3";

Echo $dog->name;

$dog->jiao ();

Seven, inheritance

Class Hashiqi extends dog# Huskies inherit the category of dogs, Husky is a subclass, the dog class is the parent class

{



}

Viii. The rewrite of the method final will not be inherited, written in front of the class

Class Hashiqi extends dog# Huskies inherit the category of dogs, Husky is a subclass, the dog class is the parent class

{

public static $height;

function Jiao ()

{

Pstrny::jiao (); #引用父类的方法, call methods of the method class of the parent class in the subclass using::

echo "Hello"; #另外添加的方法

}

function Gao ()

{

Echo is "so high." Self:: $height, Self for this class this represents this object

}

}

Hashiqi:: $height = 10; call a static member

echo Hashiqi:: $height;

$dog->jiao ();


Static statically belongs to the class. Rather than the object. For example, the length of the chalk is non-static and the color is static. The color belongs to the class, and the length belongs to the object.

Nine, polymorphic

Condition: A reference to the parent class must be inherited, pointing to an instance of the subclass, overriding the function.

The ToString method, which can display the information in the method when the object is output

$r 1 =clone $ren cloning objects

It is automatically executed at the time of cloning, and the purpose of the function is to make some changes to the cloned object.

Introduce page include ("china.php") #同一页面

Require_once "china.php"

Include ("class/china.php")

Include (".. /china. PHP ")

Auto Load

function __autoload ($classname)

{

Require_oncce $classname. ". php"

}

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.