How to learn about classes and objects in PHP

Source: Internet
Author: User
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

<?php  class A  {  function foo ()  {  if (isset ($this)) {  echo ' $this is defined (';  echo Get_class ($this);  echo ") \ n";  } else {  echo "\ $this is defined.\n";}  }  }  Class B  {  function bar ()  {  a::foo ();  }  }  $a = new A ();  $a->foo ();  A::foo ();  $b = new B ();  $b->bar ();  B::bar ();  ? >

The example above will output:

$this is defined (a)  $this are not defined.  $this is defined (b)  $this are not defined.

Example#2 a simple class definition

<?php  class Simpleclass  {  //Member declaration public  $var = ' a default value ';  Method declares public  function Displayvar () {  echo $this->var;  }  }  ? >

Default values for Example#3 class members

<?php  class Simpleclass  {  //Invalid classes member definition: public  $var 1 = ' hello '. ' World ';  Public $var 2 = <<<eod  Hello world  EOD;  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 null  var_dump ($instance);  Var_dump ($reference);  Var_dump ($assigned);  ? >

The copy code example will output:

Null  null  object (Simpleclass) #1 (1) {  ["var"]=> string "$assigned would have the this value"  }

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 Extendclass extends Simpleclass  {  //Redefine The parent method  function Displayvar ()  {  echo "extending class\n";  Parent::d Isplayvar ();  }  }  $extended = new Extendclass ();  $extended->displayvar ();  ? >

The example above will output:

Extending class  a default value
Related Article

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: 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.