【发布时间】:2016-05-22 07:07:10
【问题描述】:
我对类型提示和命名空间没有太多直观的了解。所以我编写了以下代码来处理这两个概念。我有三个 php 页面,在同一目录中包含三个类。它们是--
1.Student.php
2.机构.php
3.enroll.php.
我想在enroll 类中同时使用Student and Institution 类。我在Student and Institution 类中应用了namespaces。而use 它们在注册类中。但是这里有些不太对劲。我收到这些错误:
警告:非复合名称“Student”的 use 语句没有 在第 2 行的 C:\xampp\htdocs\practice\typehint\enroll.php 中的效果
警告:非复合名称“Institute”的 use 语句没有 在第 3 行的 C:\xampp\htdocs\practice\typehint\enroll.php 中的效果
致命错误:未找到班级“学生” C:\xampp\htdocs\practice\typehint\enroll.php 在第 10 行
谁能解释这里出了什么问题以及我该如何解决这个问题?
student.php
namespace Student;
class Student{
public $name;
publci function __construct($value){
$this->name=$value;
}
}
institute.php
namespace Institute;
class Institute{
public $institute;
public function __construct($val){
$this->institute=$val;
}
}
enroll.php
use Student;
use Institute;
class enroll{
public function __construct(Student $student,Institute $institute){
echo $student->name.' enrolled in '.$institute->institute.' school .';
}
}
$student=new Student('zami');
$institute=new Institute('Government Laboratory High School');
$enroll=new enroll($student,$institute);
【问题讨论】:
标签: php namespaces type-hinting