douduandiao1368 2016-11-08 14:01
浏览 235
已采纳

PHP可以在两个文件中嵌套类吗?

I know that php can't nest classes, but it seems that it can if the two classes are in two files:

MainClass.php:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

class mainclass
{
  private $var;
  public function __construct()
  {
    require_once ('subclass.php');
    $subinstant = new subclass();
  }
}
$maininstant = new mainclass();

And subclass.php

<?php
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
class subclass
{
  public function __construct()
  {
    $this->var="this-var
";
    echo $this->var;
    $test="test in construct
";
    echo $test;
    function testvar()
    {
      //$this->var = "this-var in fun
";
      //echo $this->var;
      $funtest="test in fun
";
      echo $funtest;
    }
    testvar();
  }
}

No errors output, and the result is as expected. I just don't understand the require_once, it seems that it will include the code in subclass.php, so what's the difference, compared to write it directly at the same position?

What's more, how can I access the variable $var in testvar()? (Use as many methods as you can think), thanks!

  • 写回答

2条回答 默认 最新

  • dongqian7545 2016-11-08 14:15
    关注

    require_once does not include the code from the other class right on the spot. The behavior of require, require_once, include and include_once is unique: it "leaves" the current object and "merges" the code from the requested file to the global scope.

    This means, that what you perceive as this

    class mainclass
    {
      private $var;
      public function __construct()
      {
        require_once ('subclass.php');
        $subinstant = new subclass();
      }
    }
    

    the PHP interpreter actually sees this (when you create an instance of mainclass):

    class mainclass
    {
      private $var;
      public function __construct()
      {
        $subinstant = new subclass();
      }
    }
    
    class subclass
    {
      public function __construct()
      {
        $this->var="this-var
    ";
        echo $this->var;
        $test="test in construct
    ";
        echo $test;
        function testvar()
        {
          //$this->var = "this-var in fun
    ";
          //echo $this->var;
          $funtest="test in fun
    ";
          echo $funtest;
        }
        testvar();
      }
    }
    $maininstant = new mainclass();
    

    As @Federkun pointed in the comment to this post, it's actually written in the docs:

    However, all functions and classes defined in the included file have the global scope.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模
  • ¥20 用vivado写数字逻辑实验报告撰写,FPGA实验
  • ¥15 为什么shp文件会有这种小方块?