Home  >  Article  >  Backend Development  >  可否在不继承一个类的时候执行一些方法

可否在不继承一个类的时候执行一些方法

WBOY
WBOYOriginal
2016-06-13 13:13:09778browse

能否在不继承一个类的时候执行一些方法?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
class a
{
   public $table = '';

   public function __contruct($table)
   {
       $this->table=$table;
   }
}

class b
{
    $b = new a('abc');//Fatal error: Class 'a' not found
}



能否使用别的什么方法代替?我现在想在b类里完成对a类构造函数的初始化

------解决方案--------------------
require_once ‘class_a_file.php’;
------解决方案--------------------
上面说的对,出错并不是不能new,而是class a的代码没加进来

我经常在一个类里面new另一个
------解决方案--------------------
探讨

靠,搜跌撕裂啊、我一直以为这个加载是应该让PHP从内部就给我完成的,没想到

------解决方案--------------------
取个例子,更好说明一些.
假设 class a 的代码存在 class_a.php class b 的代码存在class_b.php 上.
PHP code

<?php function __autoload($class_name) {
      require("class_".$class_name.".php");
}

$b = new b(); //这样会自动去 加载 class_b.php 文件
$a = new a(); //这样会自动去 加载  class_a.php 文件

?> <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn