Php object-oriented BASICS (3) and php object-oriented Basics

Source: Internet
Author: User
Tags iusb

Php object-oriented BASICS (3) and php object-oriented Basics

1. Static *
Common Member: belongs to the object
Static member: belongs to the class
Keyword: static
Class FenBi
{
Public $ length = 10; // chalk length, common member
Public static $ color = "red"; // chalk color, static member

Public static function test ()
{
Echo "static member method". self: $ color; // self class, which can also be implemented using the class name FenBi
}
Public function ceshi ()
{
Echo self: $ color;
}
}
$ F = new FenBi ();
$ F-> length; // call method of common members
$ F-> ceshi ();
FenBi: $ color; // call method of static members
FenBi: test (); // static member method. Common members cannot be called.


2. Abstraction
Keywords: abstract
Abstract classes cannot be instantiated because they are too abstract. They exist as parent classes.
The abstract method cannot contain the function body. Its specific functions are implemented in the subclass through rewriting.
Classes that contain abstract methods must be abstract classes.

Abstract class DongWu
{
Public $ name;
Abstract public function test (); // The function body does not need to be added because it is too abstract {}
}
Class Dog extends DongWu
{
Public function test () // rewrite
{
Echo "Rewrite ";
}
}
$ D = new Dog ();
Var_dump ($ d );


3. classes with extremely abstract Interfaces
No member variables in the interface
All member functions in the interface are abstract
Interface IUSB
{
Public function read (); // read Method
Public function write (); // write Method
}
Class ShuBiao implements IUSB
{
Public function read ()
{
Echo "insert mouse ";
}
Public function write ()
{
Echo "write content to the mouse ";
}
}
$ S = new ShuBiao ();
$ S-> read ();

Related Article

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.