PHP Object-oriented

Source: Internet
Author: User

<?php

Package __set () __get () __isset () __unset ()
The __get () function ==> represents a call to a private property and does not call the method if it accesses the generic member property
/*class person{
Private $age;
function __get ($var) {
echo "hahaha". $var;//var represents the variable being called
return $this $var;
}
}
$p =new person ();
Echo $p->age;
Class person{
Private $name;
Private $age;
function __get ($var) {
if ($var = = "Name")
return $this->name;
if ($var = = "Age")
return $this->age;
}
function __set ($var, $value) {
if ($var = = "Name")
return $this->name= $value;
if ($var = = "Age")
return $this->age= $value;
}
}
Class Student extends person{
function Say ($AVR) {
echo $this->name. " Say ". $avr;
}
}
$s =new Student ();
$s->__set ("name", "Zhang San");
$s->say ("haha haha");
The static property ==> This class name call can use self, but externally it has to be called with the class name
Class person{
var $name;
Static $country = "China";
function Say () {
echo "I am." Self:: $country. " Person ";//person:: $country
}
}
$p =new person ();
Person:: $country = "Alien";
$p->say ();
The static method can be called by either of the following methods
Class person{
var $name;
Static $country = "China";
function Say () {
echo "I am." Self:: $country. " People ";
}
}
Person::say ();//Recommended Use
$p =new person ();
$p->say ();
CONST constant Modifier
Class person{
var $name = "I";
Const country= "China";//constants Cannot change values
function Say () {
echo "I am". Self::country. " People ";
}
}
Person::say ();
The value of a string must be returned when the __tostring () function ==> is defined
Class person{
var $name = "I";
function __tostring () {
Return "This is an object type and cannot be output directly";
}
}
$p =new person ();
Echo $p;
The __clone () function ==> clones a new object with the same contents as the two objects, but not the same address in the heap
Class person{
var $name = "I";
function __clone () {
$this->name= "is clone out of copy";
}
}
$p =new person ();
$e =new person ();
$e =clone $p;
Echo $p->name;
Echo $e->name;
__call () function ==> can avoid program crash, only output a hint content
Class person{
var $name = "I";
function __call ($funname, $args) {//The first parameter is the method name of the call. The second argument is the one passed when the method is called.
echo $funname. " method does not exist <br> ";
Print_r ($args);
}
}
$p =new person ();
$p->fun (a);
echo "<br> program not Crashing";
Abstract methods, a class has abstract methods are abstract classes, abstract classes can not be instantiated, can only be implemented by inheritance
Abstract class demo{
Abstract function fun1 ();
function fun2 () {
echo "hahaha";
}
}
Class Test extends demo{
function Fun1 () {
echo "Overlay abstract method";
}
}
Test::fun1 ();
$t =new Test ();
$t->fun1 ();
An interface is a special kind of abstract class.
All member properties must be constants, all methods are abstract, and all members must be public
Class demo{

}
Interface lone{
Const b= "HHHH";
function Fun ();
}
Class One extends Demo implements lone{
function Fun () {
echo "haha haha";
}
}
One::fun (); */

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.