Command.interface.php:<?php
/**
 * 下命令
 * 顾客 -> 服务员 -> 厨师
 */
interface Command {
public function execute();
}
?>Cooker.class.php:<?php
/**
 * 厨师
 *
 */
class Cooker {
public function meal() {
echo '番茄炒鸡蛋'.PHP_EOL;
}
public function drink() {
echo '紫菜蛋花汤'.PHP_EOL;
}
}
?>Waiter.class.php:<?php
/**
 * 服务员
 *
 */
file_exists('Command.interface.php') ? (require 'Command.interface.php') : die('找不到Command.interface.php文件!');
file_exists('../CookMenu.class.php') ? (require '../CookMenu.class.php') : die('找不到CookMenu.class.php文件!');
file_exists('Cooker.class.php')     ? (require 'Cooker.class.php')   : die('找不到Cooker.class.php文件!');class Waiter implements Command {
private $_cook = null;
private $_func = '';

/*public function __construct(Cooker $cook) {
$this->_cook = $cook;
}*/
/**
 * 菜单的选择
 *
 */
public function chooseMenu($menu) {
$classname = ucwords($menu).'Menu';
$this->_cook = new $classname;
$this->_func = $menu; // 选择
}

/**
 * 叫厨师制作相应的食物
 *
 */
public function execute() {
$this->_cookie != null  ||  die('$this->_cookie为null');
$this->_cook->$menu();
}
}
?>CookMenu.class.php:<?php
/**
 * 菜谱
 *
 */
file_exists('Interface/Cooker.class.php') ? (require 'Interface/Cooker.class.php') : die('找不到Interface/Cooker.class.php文件!');class CookMenu {
}// 两个菜单class DrinkMenu extends CookMenu {
return new Cookie;
}
class MealMenu extends CookMenu {
return new Cookie;
}
index.php:<?php
/**
 * 开始点菜
 * 召唤服务员 -> 通过服务员进行菜单的选择(点菜) -> 由服务员带着被选择的菜单通知厨师 -> 厨师ok
 */
file_exists('Interface/Waiter.class.php')  ? (require 'Interface/Waiter.class.php')   : die('找不到Interface/Waiter.class.php文件!');$callWaiter = new Waiter; // 召唤服务员
$callWaiter->chooseMenu('drink'); // 选择菜单
?>
目录:
我的开发环境是centos 5.6,我已经把mytest文件夹(包括里面的所有文件及文件夹)都设置为777的权限了,就算把htdocs也设置为777也不行,还是提示“找不到Command.interface.php文件”。PHPfile_exists 文件 权限

解决方案 »

  1.   

    使用相对路径时,以入口文件为准.
    即访问index.php时,所有的相对路径都是相对于index.php而言的.所以require "Interface/Command.interface.php";才行.
      

  2.   

    所有形如
    file_exists('Interface/Cooker.class.php') ? (require 'Interface/Cooker.class.php') : die('找不到Interface/Cooker.class.php文件!');
    的,一律改为这样的形式
    require_once 'Interface/Cooker.class.php');特别注意的是
    file_exists('../CookMenu.class.php') ? (require '../CookMenu.class.php') : die('找不到CookMenu.class.php文件!');
    应写作
    require_once 'CookMenu.class.php';php 的搜索路径的顺序是:当前目录 -> 被引入文件目录
      

  3.   

    相对根目录文件,require_once()同理。
      

  4.   

    哦。这我明白了但是应该怎么样写这个程序呢?我总觉得怎么写都写得不大好的麻烦各位可以帮我写一份吗??感谢不尽了。。就按着这图的意思写:这幅图写错了一点:Command.class.php应该是Command.interface.php,它只是一个接口而已,定义相应的动作
      

  5.   

    所有的类(包括接口)都统一放到一个目录中
    在配置文件中引入 __autoload 函数定义
    用 __autoload 函数动态加载类定义,因为位置是固定的,所以不会弄错于是你的 index.php 就变成
    <?php
    /**
     *    开始点菜
     *    召唤服务员 -> 通过服务员进行菜单的选择(点菜) -> 由服务员带着被选择的菜单通知厨师 -> 厨师ok
     */
    include 'config.php'; 
    $callWaiter = new Waiter;            // 召唤服务员
    $callWaiter->chooseMenu('drink');    // 选择菜单
    ?>