Learning classes and objects for beginners in PHP (1)

Source: Internet
Author: User
Tags php programming php programming language

This article introduces the basic knowledge of classes and objects in the PHP programming language, which is suitable for beginners to read and hope to help you.

PHP 5 introduces a new object model. Completely rewrite the way PHP handles objects, allowing for better performance and more features.

First, the basic concept

1. Class

The definition of each class begins with the keyword class followed by the class name, which can be the name of any non-PHP reserved word. Followed by a pair of curly braces containing the definitions of the class members and methods. The pseudo-variable $this can be used when a method is called inside an object. A $this is a reference to the calling object (usually the object to which the method belongs, but it can also be another object if the method is called statically from within the second object). Look at the following example:

Example#1 $this variables in object-oriented languages

<?PHPclassA {functionfoo () {if(isset($this)) {            Echo' $this is defined ('; Echo Get_class($this); Echo") \ n"; } Else {            Echo"\ $this is not defined.\n"; }     } } classB {functionBar () {A::foo (); } } $a=NewA ();$a-foo (); A::foo ();$b=NewB ();$b-Bar (); B::bar ();?>

The example above will output:

    1. $this is defined (a)
    2. $this is not defined.
    3. $this is defined (b)
    4. $this is not defined.

Example#2 a simple class definition

<? PHP class Simpleclass {    //  member declaration public    $var = ' A default value ';     //  method declaration public    function  Displayvar () {          echo$thisvar;    ? >

Default values for Example#3 class members

<?PHPclassSimpleclass {//Invalid class member definition:     Public $var 1= ' Hello '. ' World ';  Public $var 2= <<<Eodhello Worldeod;  Public $var 3= 1 + 2;  Public $var 4= Self::Mystaticmethod ();  Public $var 5=$myVar; //the correct class member definition:     Public $var 6=myconstant;  Public $var 7= Self::classconstant;  Public $var 8=Array(true,false);} ?>

2. New

To create an instance of an object, you must create a new object and assign it to a variable. When a new object is created, the object is always assigned, unless the object defines the constructor and throws an exception when an error occurs.

Example#4 Creating an instance

<? PHP   $instance New Simpleclass ();  

Copy code when you assign an instance of an object that has already been created to a new variable, the new variable accesses the same instance and assigns a value to that object. This behavior is the same as when the function is passed into the instance. You can use clones to create a new instance of an object that has already been created.

Assigning values to example#5 objects

<?PHP$assigned=$instance;$reference= &$instance;$instance-var= ' $assigned'll has this value ';$instance=NULL;//$instance and $reference become nullVar_dump($instance);Var_dump($reference);Var_dump($assigned);?>

The copy code example will output:

    1. Null
    2. Null
    3. Object (Simpleclass) #1 (1) {
    4. ["var"]=>
    5. String ("$assigned would have the this value"
    6. }

3, extends

A class can inherit the methods and members of another class in a declaration with the extends keyword. You cannot extend multiple classes, only one base class can be inherited.

Inherited methods and members can be overwritten by a re-declaration with the same name, unless the parent class defines the method with the final keyword. The overridden method or member can be accessed through the parent::.

Example#6 Simple class inheritance

<? PHP class extends Simpleclass {    //  Redefine The parent method    function  Displayvar () { c10/>echo "Extending class\n";        :: Displayvar ();     $extended New Extendclass (); $extended  -Displayvar ();? >

The example above will output:

    1. Extending class
    2. A default value

Learning classes and objects for beginners in PHP (1)

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.