PHP Object-oriented

Source: Internet
Author: User

I. Classes and objects

Object-oriented:
① Defining classes
Class Dog
{

② declaring member variables
var $name;
var $age;
var $pinzhong;
③ declaring member functions
function Jiao ()
{
echo "{$this->name} is called";
}
}

④ instantiating an Object
$dog = new Dog ();

⑤ Object Member Value Assignment

$dog->name;

⑥ the members of the calling object:
$dog->name = "Wang Choi";
$dog->jiao ();

Two: $this key words

$this is the reference inside the object that represents the object.

echo "This person is called:". $this->name;

This represents the object, and if you want to invoke a member variable in a member method, be sure to use $this to find the member variable

Three: constructors:

PHP5 before the class name, PHP5 changed to __construct ()

Defining methods
function __construct ($n, $a, $p)
{
$this->name = $n;
$this->age = $a;
$this->pinzhong = $p;
}

The constructor method is common public by default

Overloading: PHP is not overloaded because it is a weakly typed language


Four: Destructors:

function __destruct () {echo "Goodbye";}


Five: Package:

① access modifier: private,public,protected
② adds a private modifier to a member variable inside a class to protect its members from arbitrary access to the outside world, ensuring class security. 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;
}

③ as long as there are other keywords in front of the member property to remove the original keyword var

Private $name;

VI: Inheritance
① inheritance is generally a single inheritance: a son can only have one father
② subclasses can inherit all members of the parent class
The ③ subclass can override a member method of the parent class: (same as the function name), if you want to invoke the method of the parent class in the subclass method, use Parent:say () or Person::say (), to implement the original method of calling the parent class

Parent::Jiao (); function Jiao () {parent::Jiao (); Echo "Hello";}

Subclasses can be overridden with the same function name

function Say ()        {            parent::Say ();             Echo "Methods in subclasses";        }    }


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
The class takes static variables, cannot use $this, so use self

⑥const keywords

const When a keyword that defines a constant

Defining Constants in PHP uses the "define ()" function, but defining constants in a class uses the keyword "const"

Assignment value is the same as static

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.