PHP Object-oriented

Source: Internet
Author: User

Object-oriented:


One: Define Class
Class Dog
{
var $name;
var $age;
var $pinzhong;

function Jiao ()
{
echo "{$this->name} is called";
}
}

Two: Instantiating an object
$dog = new Dog ();
Call the members of the object:
$dog->name = "Wang Choi";
$dog->jiao ();

Three: $this keywords
$this->name;this represents the object, if you want to call member variables in a member method, be sure to use $this to find the member variable

Four: constructor:
function __construct ($n, $a, $p)
{
$this->name = $n;
$this->age = $a;
$this->pinzhong = $p;
}
Five: Destructors:
function __destruct ()
{
echo "Goodbye";
}


Six: Package:
Adding a private modifier to a member variable inside a class is intended 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 the function Get function set function, then assign and value the variable
2. Use the system's own __get () function and the __set () function to implement the value of the variable assignment within the class.
function __set ($name, $value)
{
$this $name = $value;
}
function __get ($name)
{
return $this $name;
}

Seven: Inheritance
Inheritance is generally a single inheritance: a son can have only one father.
Subclasses can inherit all members of the parent class
A subclass can override a member method of a parent class: (like a function name), if you want to invoke a method of the parent class in a subclass method, use Parent::jiao ();
function Jiao ()
{
Parent::jiao ();

echo "Hello";
}
The final keyword can define the class as the final class and cannot be inherited

Static:
Keyword: static, if the member declares to be static, then the member belongs to the class, not to the object.
How to invoke a static member:
Hashiqi:: $height = 10;
Hashiqi::gao ();
To invoke a static member of a class with a class name and double colons

Polymorphic :
Condition: must inherit, reference of parent class to child class instance, overriding function

function __tostring ()
{
echo "This is a human being";
}
The ToString method, which can display the information in the method when the object is output

$r 1 = clone $ren; Clone object
function __clone ()
{
$this->name = "John Doe";
}

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

To reference a class on a page:

1.include To reference
Include (".. /china.php ");

2.require_once Request page
Require_once ". /china.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.