zoukankan      html  css  js  c++  java
  • 关于$->aaa->bbb();的困惑

    第21行为什么可以调用test类的aa方法呢?

      答:因为前一行(20)其已经被实例化了。所以现在的$this->obj其实可以相当于是一个对象。

      20行和21行也可以写成如下
      $xxoo = new test();
      $xxoo -> a();
      这样而是等同
      $this->obj = new test();
      $this->obj -> aa();

     1 <?php
     2     class test{
     3         public $a=1;
     4         public function aa() {
     5             return 1;
     6         }
     7          
     8         public function bb() {
     9             //this就是test对象
    10             echo $this->aa();    //这个指的是调用当前对象的aa方法
    11             echo $this->a;    //这个指的是调用a属性
    12         }
    13     } 
    14      
    15      
    16     class test1{
    17         public $test1 = 1;
    18         public $obj;
    19         public function test2() {
    20             $this->obj = new test();//实例化test对象
    21             $this->obj->aa();//调用test的aa方法,
    22             $this->obj->a;//调用test的a
    23             $this->test1;//test1 的 test1属性
    24         }
    25     }
    26 ?>
  • 相关阅读:
    Angular
    Angular
    Angular
    Angular
    Angular
    Angular
    Angular
    springsecurity 源码解读 之 RememberMeAuthenticationFilter
    springsecurity 源码解读之 AnonymousAuthenticationFilter
    springsecurity 源码解读之 SecurityContext
  • 原文地址:https://www.cnblogs.com/nul1/p/8647989.html
Copyright © 2011-2022 走看看