PHP beginner's learning class and object (1) _ PHP Tutorial

Source: Internet
Author: User
PHP beginner's learning class and object (1 ). PHP5 introduces a new object model (ObjectModel ). The PHP object processing method is completely rewritten, allowing better performance and more features. I. basic concepts 1. definition of each class PHP5. a new Object Model is introduced ). The PHP object processing method is completely rewritten, allowing better performance and more features.

I. Basic concepts

1. class

The definition of each class starts with the keyword class followed by the class name. it can be the name of any non-PHP reserved word. Followed by a pair of curly braces, the bread contains class member and method definitions. The pseudo variable $ this can be used when a method is called inside the object. $ This is a reference to the calling object (usually the object of the method, but it can also be another object, if the method is called statically from the second object. Take the following example:

Example #1 $ this variable in object-oriented language

 
 
  1. class A
  2. {
  3. function foo()
  4. {
  5. if (isset($this)) {
  6. echo '$this is defined (';
  7. echo get_class($this);
  8. echo ")n";
  9. } else {
  10. echo "$this is not defined.n";
  11. }
  12. }
  13. }
  14. class B
  15. {
  16. function bar()
  17. {
  18. A::foo();
  19. }
  20. }
  21. $a = new A();
  22. $a->foo();
  23. A::foo();
  24. $b = new B();
  25. $b->bar();
  26. B::bar();
  27. ?>

The above example will output:

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

Example #2 simple class definition

 
 
  1. Class SimpleClass
  2. {
  3. // Member declaration
  4. Public $ var = 'A default value ';
  5. // Method declaration
  6. Public function displayVar (){
  7. Echo $ this-> var;
  8. }
  9. }
  10. ?>

Example # default value of Class 3 members

 
 
  1. Class SimpleClass
  2. {
  3. // Invalid class member definition:
  4. Public $ var1 = 'hello'. 'world ';
  5. Public $ var2 = <
  6. Hello world
  7. EOD;
  8. Public $ var3 = 1 + 2;
  9. Public $ var4 = self: myStaticMethod ();
  10. Public $ var5 = $ myVar;
  11. // Correct class member definition:
  12. Public $ var6 = myConstant;
  13. Public $ var7 = self: classConstant;
  14. Public $ var8 = array (true, false );
  15. }
  16. ?>

2. new

To create an instance of an object, you must create a new object and assign it to a variable. 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.

Example #4 Create an instance

 
 
  1. $instance = new SimpleClass();
  2. ?>

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 #5 assign values to objects

 
 
  1. $assigned = $instance;
  2. $reference =& $instance;
  3. $instance->var = '$assigned will have this value';
  4. $instance = null; // $instance and $reference become null
  5. var_dump($instance);
  6. var_dump($reference);
  7. var_dump($assigned);
  8. ?>

The above example will output:

 
 
  1. NULL
  2. NULL
  3. object(SimpleClass)#1 (1) {
  4. ["var"]=>
  5. string(30) "$assigned will have this value"
  6. }

3. extends

A class can inherit the methods and members of another class with the extends keyword in the declaration. You cannot expand multiple classes and can inherit only one base class.

The inherited methods and members can be overwritten by re-declaring with the same name, unless the final keyword is used when the parent class defines the method. You can use parent: To access covered methods or members.

Example #6 simple class inheritance

 
 
  1. class ExtendClass extends SimpleClass
  2. {
  3. // Redefine the parent method
  4. function displayVar()
  5. {
  6. echo "Extending classn";
  7. parent::displayVar();
  8. }
  9. }
  10. $extended = new ExtendClass();
  11. $extended->displayVar();
  12. ?>

The above example will output:

 
 
  1. Extending class
  2. a default value

1

Http://www.bkjia.com/PHPjc/445764.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445764.htmlTechArticlePHP 5 introduces a new Object Model ). The PHP object processing method is completely rewritten, allowing better performance and more features. I. basic concepts 1. definition of each class of class...

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.