Let's take a closer look at common PHP object-oriented keywords and magic methods.

Source: Internet
Author: User

Let's take a closer look at common PHP object-oriented keywords and magic methods.

Common keywords in PHP object-oriented 

Final

1. final cannot modify member attributes (constants in the class do not use this keyword)

2. final can only modify classes and Methods

Purpose:

Classes modified using final cannot inherit from quilt classes.

The final modification method cannot be overwritten by the quilt class.

Used to restrict the class from being inherited, and use final if the method is not overwritten

<? Php // The final modified class cannot be inherited from final class Person {var $ name; var $ age; var $ sex; function _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function fun1 () {echo $ this-> name ;}// Student class inherits the Person class modified by final, so class Student extends Person {} $ stu = new Student ("zs", 20, "nan"); $ stu-> fun1 ();?>

Static (static keyword)

1. You can use static to modify member attributes and member methods, but not classes.

2. The member attributes modified with static can be shared by all objects of the same class.

3. static data is stored in the data segment in the memory (initialize the static segment)

4. static data is allocated to the memory when the class is loaded for the first time, and will be directly obtained from the data segment when the class is used again.

5. What is a class loaded? As long as this class is used in the Program (this class name appears)

6. static methods (static modification method). Users cannot access non-static members (static members can be accessed in non-static methods)

Because non-static members must be accessed using objects, $ this is used to access internal members. Static methods do not need to be called by objects, so no objects exist, $ this does not represent objects. Non-static members must also use objects.

If you are sure that a method does not use non-static members, you can declare this method as a static method.

Note: static members must be accessed by class names. Do not create objects or access objects.

Class Name: static member

If you use static members in the class, you can use self to represent the class.

Const

1. It can only modify member attributes

2. Declare the constant attribute in the class using const

3. The access method is the same as the static member attribute (use Class Name: constant outside the class to use self: constant inside the class)

4. constants must be declared with the initial value.

<? Php // define a class "people" class Person {protected $ name; protected $ age; protected $ sex; static $ country = "China "; // declare a constant const RUN = "walk"; // constructor _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function getCountry () {// If a static member is used in the class, you can use self to represent the return self: $ country;} function say () {echo "my name: {$ this-> name}, my age: {$ this-> age}, my gender: {$ this-> sex }. <Br> ";}protected function eat () {echo" dinner! <Br> ";} function run () {// use the constant self: constant echo self: RUN in the class. "<br>" ;}// declare the static method static function hello () {echo "hello <br> ";}}

Common magic methods in PHP object-oriented

_ Call ()

Purpose: When you call a method that does not exist in an object, the system reports an error and the program exits.

When to call automatically: it will automatically call a method that does not exist in an object.

Handle nonexistent error calls

This method requires two parameters.

<? Php // define a class "people" class Person {protected $ name; protected $ age; protected $ sex; static $ country = "China "; // declare a constant const RUN = "walk"; // constructor _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function getCountry () {// If a static member is used in the class, you can use self to represent the return self: $ country;} function say () {echo "my name: {$ this-> name}, my age: {$ this-> age}, my gender: {$ this-> sex }. <Br> ";}protected function eat () {echo" dinner! <Br> ";} function run () {// use the constant self: constant echo self: RUN in the class. "<br>";} // handle some nonexistent errors. call // The function _ call ($ methodName, $ args) {// $ methodName: The method name for calling a method without a method $ echo parameter in args "The method you call {$ methodName} (parameter:"; print_r ($ args ); echo ") does not exist <br>" ;}// declare static method static function hello () {echo "hello <br> ";}} $ p = new Person ("Zhang San", 20, "female"); $ p-> test (, 30); $ p-> demo ("aa ", "bb"); $ p-> say ();?>

_ ToString ()

This function is automatically called when an object is referenced directly. It is used to quickly obtain the fastest way to represent a string.

<? Php // define a class "people" class Person {protected $ name; protected $ age; protected $ sex; static $ country = "China "; // declare a constant const RUN = "walk"; // constructor _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {echo "my name: {$ this-> name }, my age: {$ this-> age}, my gender: {$ this-> sex }. <Br> ";} function _ toString () {return self: $ country. "<br >{$ this-> name} <br >{$ this-> age }< br >{$ this-> sex} <br> ". self: RUN; }}$ p = new Person ("Zhang San", 21, "female"); echo $ p;?>

_ Clone ()

Clone an object using clone ()

Original (original object)

Duplicate (Copied object)

_ Clone () is the method automatically called when the object is cloned.

As long as an object is created, Initialization is required, which is similar to constuct.

In the _ clone () method, the $ this keyword indicates the duplicate object, and $ that indicates the original object.

<? Php // defines a class "people" class Person {var $ name; protected $ age; protected $ sex; static $ country = "China "; // declare a constant const RUN = "walk"; // constructor _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {echo "my name: {$ this-> name }, my age: {$ this-> age}, my gender: {$ this-> sex }. <Br> ";}function _ clone () {$ this-> name =" Wang Wu "; $ this-> age = 18; $ this-> sex = "male";} function _ destruct () {echo $ this-> name. "<br>" ;}}$ p = new Person ("Zhang San", 21, "female"); $ p-> say (); // This cannot be called a cloned object, because only one time is destructed/* $ p1 = $ p; $ p1-> name = "Li Si "; $ p1-> say (); */$ p1 = clone $ p; $ p1-> say ();?>

_ Autoload ()

Note: Other magic methods are added to the class. This is the only method not added to the class.

If a class is used on the page and the class name is used, the class name is automatically passed to this parameter.

<?phpfunction __autoload($className){  include "./test/".$className.".class.php";}  $o=new One;  $o->fun1();    $t=new Two;  $t->fun2();  $h=new Three;  $h->fun3();?>

Files in test

One. class. php

<?phpclass One{  function fun1(){    echo "The Class One<br>";  }}?>

Two. class. php

<?phpclass Two{  function fun2(){    echo "The Class Two<br>";  }}?>

Three. class. php

<?phpclass Three{  function fun3(){    echo "The Class Three<br>";  }}?>

Object serialization: convert an object into a binary string (the object is stored in the memory and easy to release)

Usage time:

1. Storing objects in a database or file for a long time

2. When an object is transmitted in multiple PHP files

Serialize (); the parameter is an object. The returned value is the serialized binary string.

Unserialize (); the parameter is the binary string of the object. The returned value is the newly generated object.

_ Sleep ()

Is the method called during serialization

Function: Partially serializes an object.

As long as an array is returned in this method, several member attributes are serialized in the array. If this method is not added, all members are serialized.

_ Wakeup ()

Is the method called during deserialization

It is also the process of re-emergence of Objects

<? Php // defines a class "people" class Person {var $ name; protected $ age; protected $ sex; static $ country = "China "; // declare a constant const RUN = "walk"; // constructor _ construct ($ name, $ age, $ sex) {$ this-> name = $ name; $ this-> age = $ age; $ this-> sex = $ sex;} function say () {echo "my name: {$ this-> name }, my age: {$ this-> age}, my gender: {$ this-> sex }. <Br> ";}function _ clone () {$ this-> name =" Wang Wu "; $ this-> age = 18; $ this-> sex = "male";} // is the method called during serialization. You can partially serialize the object function _ sleep () {return array ("name ", "age");} // It is a method called during deserialization and a process of re-emergence of an object. You can change the value of function _ wakeup () {$ this-> name = "sanzhang"; $ this-> age = $ this-> age + 1 ;} function _ destruct () {}}?>

Read. php

<?php  require "11.php";    $str=file_get_contents("mess.txt");  $p=unserialize($str);  echo $p->say();?>

Write. php

<? Php require "11.php"; $ p = new Person (" Zhang San ", 18," male "); $ str = serialize ($ p); file_put_contents (" mess.txt ", $ str);?>

The above detailed discussion about the common keywords and magic methods in PHP object-oriented is all the content shared by xiaobian. I hope you can give us a reference and support for the customer's house.

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.