A case study of PHP class _php skills

Source: Internet
Author: User

The examples in this article describe the characteristics of the PHP class. Share to everyone for your reference, specific as follows:

Object down-Passing attribute

When an object invokes an instance method and then statically invokes the method of another class in the method, the object in the source method is obtained in the statically called method (this)

<?php
class bee{public
  $a = 1;
  Public function f () {
    echo $this->a;
    Echo ' <br> ';
    @lig:: F ();
  }
}
Class lig{public
  $a = 2;
  Public function f () {
    echo $this->a;
  }
}
$obj = new Bee ();
$obj->f ();

The results of the operation are:

1
1

Static late statically binding

Static has 3 different meanings of syntax

Static variables in a function

function f () {
  static $v = 1;
}

Static members in a class

Class bee{
  static $v = 1;
  static function f () {}
}

dynamically specifies ' current class ' in the method, unlike self, static represents the class (dynamic) that calls this method, and self refers to the class (static) where the code resides

Class bee{
  static public $a = ten;
  static public function f () {
    echo get_class (). ': ';
    echo Self:: $a. '-';
    echo static:: $a;
  }
}
Class Lig extends bee{
  static public $a =;
}
Echo Bee::f ();
Echo ' <br> ';
Echo Lig::f ();

The results of the operation are as follows:

Bee:10-10
Bee:10-20

Three thoughts of object-oriented

1. Package

is to encapsulate the data as far as possible not to others, you can think that the most basic encapsulation is a lot of data encapsulation class inside, but more strictly speaking, is to make the attributes as private, and through a common way to provide operations.

2. Inheritance

3. Polymorphism

Usually refers to an object that obtains different results in the same way.
It also means that different objects use the same method to get different results.

PS: From PHP 5.3.0, you can use a variable to dynamically invoke the class. However, the value of the variable cannot be a keyword self,parent or static "This example tests the running environment as php5.3.3."

More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips

I hope this article will help you with your PHP programming.

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.