function show() {
    $lei = new Lei();
    $lei->set(3); //如果把此语句放在下面红色标记的后面就行
    echo $this->a;

$lei = new Lei();类里面new类的对象是错误的,没看过这种写法。
写法错误,没有类对象当然不能调用其方法咯。

解决方案 »

  1.   

    不能在里面调用function show() { 
        $lei = new Lei();  //这句代码不报错么?
        $lei->set(3); //如果把此语句放在下面红色标记的后面就行 
        echo $this->a; 
      

  2.   

    例:<?php
    class DateTime {   //定义了一个类DataTime   
       function DateTime() 
       {
           // empty constructor
       }   function now() 
       {
           return date("Y-m-d H:i:s");
       }
    }class Report {   //定义另一个类Report
       var $_dt;
       // more properties ...   function Report() 
       {
           $this->_dt = new DateTime();   //在另一个类里面可以调用类DataTime
           // initialization code ...
       }   function generateReport() 
       {
           $dateTime = $this->_dt->now();
           // more code ...
       }   // more methods ...
    }$rep = new Report();   //在类外面可以调用类Report
    ?> 
      

  3.   

    $lei = new Lei(); 
    $lei->set(3); //如果把此语句放在下面红色标记的后面就行 不知道楼主为什么要在Lei里面继续new Lei
    为什么不直接用 $this->
      

  4.   

    lz要的是这个吧?function show() {
        $this->set(3); 
        echo $this->a;

      

  5.   

    3楼可能没看明白我的意思,我的意思类里面new自己类的对象,这种写法,你看过吗?
      

  6.   

    <?php 
    class Lei { 
    var $a; 
    function set($n) { 
        $this->a =$n; 
      } 
    function show() { 
        return $this->a; 


    $lei = new Lei();     
    echo $lei->show(); ?>