包含和类PHP

时间:2015-03-27 16:59:48

标签: php class oop include

为什么php不读取我的类文件?我有这个结构:

  • home.php
  • stamp.php
  • 类/ class.stamp.php
  • 类/ class.text.php

home.php

<?php

echo "hello i'm the home page";
include 'class/class.stamp.php'
$stamp = new Stamp();

include 'class/class.text.php';

?>

class.text.php

<?php

$stamp->something('hello yes it is');

?>

class.stamp.php

<?php

class Stamp {
  public function something($text);
      echo $text;
  }
}

?>

结果呢?没有!它说:

  

致命错误:在/ bla / bla / bla /

中调用非对象上的成员函数

但如果我这样做,那就有效了!

home.php

<?php

echo "hello i'm the home page";    
include 'class/class.text.php';

?>

class.text.php

<?php

include 'class.stamp.php'
$stamp = new Stamp();
$stamp->something('hello yes it is');

?>

class.stamp.php

<?php

class Stamp {
  public function something($text) {
      echo $text;
  }
}

?>

但我不需要那个,请大家帮助我:(

1 个答案:

答案 0 :(得分:0)

检查问题是否与目录有关。

你说它适用于class.stamp.php,但不应该是class / class.stamp.php吗?例如,如果您的主页位于某个文件夹中,那么位于相同的文件夹位置,您应该拥有与Home.php文件相邻的文件夹类。

我还有未实例化对象的问题,因此请确保将其包含在每个范围内。为了帮助您发现错误,请使用

require "class/class.stamp.php";//Throws an error if it can't find the file.
require_once "class/class.stamp.php";//Bad practice

如果要求不帮助您解决问题,请发布所有内容。