Php class and object

Source: Internet
Author: User
Since PHP5, the object model has been completely rewritten to get better performance and more features. This is the biggest change since PHP4. PHP5 has a complete object model. New features in PHP5 include access control, abstract classes and final classes and methods, additional magic methods, interfaces, object replication, and type constraints. PHP treats objects in the same way as references and handles, that is, each variable holds an object reference, rather than copying the entire object.

I. INTRODUCTION

Since PHP 5, the object model has been completely rewritten to get better performance and more features. This is the biggest change since PHP 4. PHP 5 has a complete object model.
New features in PHP 5 include access control, abstract classes and final classes and methods, additional magic methods, interfaces, object replication, and type constraints.
PHP treats objects in the same way as references and handles, that is, each variable holds an object reference, rather than copying the entire object.

II. Basic concepts

1. class

The definition of each class starts with the keyword class, followed by the class name, followed by a pair of curly braces, which contains the class attributes and method definitions.
The class name can be a legal tag for any non-PHP reserved words. A valid class name must start with a letter or underline followed by letters, numbers, or underscores. The regular expression is: [a-zA-Z _ \ x7f-\ xff] [a-zA-Z0-9 _ \ x7f-\ xff] *.
A class can contain its own constants, variables (called "attributes"), and functions (called "methods ").
Example #1 simple class definition

 Var ;}}?>

When a method is called inside the class definition, there is an available pseudo variable $ this. $ This is a reference to the main object (usually the object subordinate to this method, but it may also be another object when it is called from the second object statically ).
Example #2 $ this pseudo variable Example

 ';} Else {echo' $ this is not defined.
';}}} Class B {function bar () {// If E_STRICT is enabled, A warning is displayed: A: foo ();}} $ a = new A (); $ a-> foo (); // if the E_STRICT line is enabled, A warning is displayed: a: foo (); $ B = new B (); $ B-> bar (); // If E_STRICT is enabled, a warning is displayed: bar ();?>

Output result:

$ This is defined ()
$ This is not defined.
$ This is defined (B)
$ This is not defined.

2. new

To create an instance of a class, you must use the new keyword. This object is always assigned a value when a new object is created, unless the object defines the constructor and throws an exception when an error occurs. Classes should be defined before being instantiated (this is required in some cases ).
If new is followed by a string containing the class name, an instance of the class is created. If the class belongs to a namespace, its full name must be used.
Example #3 create an instance

 

Inside the class definition, you can use new self and new parent to create new objects.
When you assign an instance created by an object to a new variable, the new variable accesses the same instance, which is the same as the value assigned by this object. This behavior is the same as when the function is passed into the instance. You can create a new instance by cloning it to a created object.
Example #4 assign values to objects

Include_once ('class1. php '); $ instance = new SimpleClass (); $ assigned = $ instance; $ reference = & $ instance; $ instance-> var =' $ assigned will have this value '; $ instance = null; // $ instance and $ reference are converted into nullvar_dump ($ instance); var_dump ($ reference); var_dump ($ assigned );

The output result is:

NULL
NULL
Object (SimpleClass) #1 (1) {["var"] => string (30) "$ assigned will have this value "}

PHP 5.3.0 introduces two new methods to create an object instance:

C

lass Test{    static public function getNew()    {        return new static;    }}class Child extends Test{}$obj1 = new Test();$obj2 = new $obj1;var_dump($obj1 !== $obj2);$obj3 = Test::getNew();var_dump($obj3 instanceof Test);$obj4 = Child::getNew();var_dump($obj4 instanceof Child);

Output result:

Bool (true)
Bool (true)
Bool (true)

3. extends

A class can use the extends keyword in the declaration to inherit the methods and attributes of another class. PHP does not support multiple inheritance. a class can only inherit one base class (same as java ).
The inherited methods and attributes can be overwritten by declaring them with the same name. However, if final is used when the parent class defines a method, the method cannot be overwritten. You can use parent: To access overwriting methods or attributes.
If the override method is used, the parameters must be consistent. otherwise, PHP will issue an E_STRICT error message. Except constructors, constructors can use different parameters when overwritten.
Example #6 simple class inheritance:

Class ExtendClass extends SimpleClass {// override the method of the parent class function displayVar () {echo "Extending class
"; Parent: displayVar () ;}$ extended = new ExtendClass (); $ extended-> displayVar ();

Output result:

Extending class
1

4.: class

Since PHP 5.5, the keyword class can also be used for parsing class names. You can use ClassName: class to obtain a string that contains the fully qualified name of the class ClassName. This is especially useful for classes that use namespaces.
Example #7 class name resolution

namespace NS{    class ClassName{}    echo ClassName::class;}

Output result:

NS \ ClassName

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.