Object-oriented PHP

Source: Internet
Author: User

I. Defining classes
      • format: class classname  [extendsclass] {varproperty  = value;        // Properties function functionname (args) {     // method    /code }}

Second, create the object

    • Creating objects using the New keyword
    • $obj=new person ();
Third, get the object in the class
    • Echo $obj // Get Properties in a class $obj // calling methods in a class
Four, const constants
    • You can define constants in a class, that is, values that do not change in the class. For any object instantiated from this class, constant values remain unchanged throughout the life cycle of those objects.
    • The class constants are created as follows:
      • Const NAME = ' value '; for example:constPI = 3.1415926;

    • echo self::pi// class internal access echo math_function::pi//  class external Access
static properties and Static methods
  • You can use the static keyword to identify member properties or to identify member methods.
  • Here's how to create it:
      • Static $test _static // Properties Static function  // method }
  • Declaring a class property or method as static can be accessed directly without instantiating the class
  • Static methods do not need to be called through an object, so the pseudo-variable $this is not available in a static method.
  • A static property cannot be accessed by an object through the operator.
  • Note: Static members belong to class-owned, so we cannot use $this to refer to static members in static methods, we recommend using the Self keyword to invoke.
  • For example:
      •  class   test{ static   $a  = ' static ' < span> static  function   m () { return  self:: $a ; //   }}   $a ; //     External access property  echo  Test::m (); //
         external access method  
VI, Inherit public--public property, can freely access private--private property or method only in their own class can use protected--protected properties: Only in their own class and subclasses can call

Prototype Properties--Implementing inheritance

Object-oriented PHP

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.