Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类型将一个类作为参数暗示给另一个具有不同名称空间的类_Php_Namespaces_Type Hinting - Fatal编程技术网

Php 类型将一个类作为参数暗示给另一个具有不同名称空间的类

Php 类型将一个类作为参数暗示给另一个具有不同名称空间的类,php,namespaces,type-hinting,Php,Namespaces,Type Hinting,我对类型暗示和名称空间没有太多的直觉知识。因此,我编写了以下代码来处理这两个概念。我有三个php页面,其中包含三个类和同一目录中的index.php文件。它们是-- 1.Student.php 2.Institution.php 3.enroll.php 4.index.php 我想在注册类中同时使用学生和机构类。我在学生和机构和注册类中应用了名称空间。并且在注册类中使用它们。但是有些地方不太正确。我得到了以下错误:“这是说我应该通过enroll\student而不是student\studen

我对类型暗示和名称空间没有太多的直觉知识。因此,我编写了以下代码来处理这两个概念。我有三个php页面,其中包含三个类和同一目录中的index.php文件。它们是--

1.Student.php

2.Institution.php

3.enroll.php

4.index.php

我想在
注册
类中同时使用
学生和机构
类。我在
学生和机构
注册
类中应用了
名称空间
。并且
在注册类中使用它们。但是有些地方不太正确。我得到了以下错误:“这是说我应该通过enroll\student而不是student\student”

可捕获致命错误:参数1传递给 enroll\enroll::\uu construct()必须是enroll\student的实例, student\student的实例已给定,已调用 第9行的C:\xampp\htdocs\practice\typehint\index.php,在中定义 第5行的C:\xampp\htdocs\practice\typehint\enroll.php

有人能解释一下这里出了什么问题以及我如何解决这个问题吗?

student.php

namespace Student;
 class Student{

     public $name;

     public function __construct($value){
        $this->name=$value;
     }

 }
namespace Institute;
   class Institute{

      public $institute;
      public function __construct($val){
         $this->institute=$val;
      }
   }
   namespace enroll;
      class enroll{

          public function __construct(Student $student,Institute $institute){
             echo $student->name.' enrolled in '.$institute->institute.' school .';
          }
      }
institute.php

namespace Student;
 class Student{

     public $name;

     public function __construct($value){
        $this->name=$value;
     }

 }
namespace Institute;
   class Institute{

      public $institute;
      public function __construct($val){
         $this->institute=$val;
      }
   }
   namespace enroll;
      class enroll{

          public function __construct(Student $student,Institute $institute){
             echo $student->name.' enrolled in '.$institute->institute.' school .';
          }
      }
enroll.php

namespace Student;
 class Student{

     public $name;

     public function __construct($value){
        $this->name=$value;
     }

 }
namespace Institute;
   class Institute{

      public $institute;
      public function __construct($val){
         $this->institute=$val;
      }
   }
   namespace enroll;
      class enroll{

          public function __construct(Student $student,Institute $institute){
             echo $student->name.' enrolled in '.$institute->institute.' school .';
          }
      }
index.php:

include 'institute.php';
include 'student.php';
include 'enroll.php';

  $institute=new institute\institute('GLAB');
  $student=new student\student('zami');
  $enroll=new enroll\enroll($student,$institute);

enroll类中的构造函数类型提示学生和学院类属于enroll命名空间,这是错误的原因。要从学生和学院命名空间类型提示类,只需使用它们:

enroll.php

namespace enroll;

use Student\Student;
use Institute\Institute;

class enroll{

    public function __construct(Student $student,Institute $institute){
        echo $student->name.' enrolled in '.$institute->institute.' school .';
    }
}

无需加载文件,因为enroll.php文件在索引中最后加载。php

enroll类中的构造函数键入提示学生和学院类属于enroll命名空间,这就是错误的原因。要键入提示来自学生和学院命名空间的类,只需使用em:

enroll.php

namespace enroll;

use Student\Student;
use Institute\Institute;

class enroll{

    public function __construct(Student $student,Institute $institute){
        echo $student->name.' enrolled in '.$institute->institute.' school .';
    }
}

无需加载文件,因为enroll.php文件在index.php中最后加载。php

正如一行开头所示:
publci-function\uuu构造($value){
在student.php中可能希望成为
public-function…
相反。抱歉,我之前已经更正了它。编辑post
public-function\uu构造(\Student\Student$Student、\Institute\Institute$Institute){
或在
名称空间注册之后应用use子句;
类似
使用\Student\Student\Institute\Institute;
将名称空间视为类似文件夹。如果您试图在Institute文件夹中打开文件Student,则该操作无效。您需要说您想要Student/Student,即Student文件夹中的Student文件,或Student cla学生名称空间中的ss。正如一行开头所示:
publci function\uu构造($value){
在Student.php中可能希望成为
public function…
。抱歉,我之前已经更正了它。.编辑post
public function\uu构造(\Student\Student$Student\Institute\Institute$Institute){
或在
名称空间注册之后应用use子句;
类似
使用\Student\Student\Institute\Institute;
将名称空间视为类似文件夹。如果您试图在Institute文件夹中打开文件Student,则该操作无效。您需要说您想要Student/Student,即Student文件夹中的Student文件,或Student cla学生名称空间中的ss。