<legend id='hZ9Oa'><style id='hZ9Oa'><dir id='hZ9Oa'><q id='hZ9Oa'></q></dir></style></legend>

    1. <tfoot id='hZ9Oa'></tfoot>
      • <bdo id='hZ9Oa'></bdo><ul id='hZ9Oa'></ul>

        <i id='hZ9Oa'><tr id='hZ9Oa'><dt id='hZ9Oa'><q id='hZ9Oa'><span id='hZ9Oa'><b id='hZ9Oa'><form id='hZ9Oa'><ins id='hZ9Oa'></ins><ul id='hZ9Oa'></ul><sub id='hZ9Oa'></sub></form><legend id='hZ9Oa'></legend><bdo id='hZ9Oa'><pre id='hZ9Oa'><center id='hZ9Oa'></center></pre></bdo></b><th id='hZ9Oa'></th></span></q></dt></tr></i><div id='hZ9Oa'><tfoot id='hZ9Oa'></tfoot><dl id='hZ9Oa'><fieldset id='hZ9Oa'></fieldset></dl></div>

        <small id='hZ9Oa'></small><noframes id='hZ9Oa'>

      1. 将命名空间与从变量创建的类一起使用

        Using namespaces with classes created from a variable(将命名空间与从变量创建的类一起使用)
        • <tfoot id='GT7iq'></tfoot>
              <bdo id='GT7iq'></bdo><ul id='GT7iq'></ul>
            • <legend id='GT7iq'><style id='GT7iq'><dir id='GT7iq'><q id='GT7iq'></q></dir></style></legend>
            • <small id='GT7iq'></small><noframes id='GT7iq'>

                    <tbody id='GT7iq'></tbody>
                  <i id='GT7iq'><tr id='GT7iq'><dt id='GT7iq'><q id='GT7iq'><span id='GT7iq'><b id='GT7iq'><form id='GT7iq'><ins id='GT7iq'></ins><ul id='GT7iq'></ul><sub id='GT7iq'></sub></form><legend id='GT7iq'></legend><bdo id='GT7iq'><pre id='GT7iq'><center id='GT7iq'></center></pre></bdo></b><th id='GT7iq'></th></span></q></dt></tr></i><div id='GT7iq'><tfoot id='GT7iq'></tfoot><dl id='GT7iq'><fieldset id='GT7iq'></fieldset></dl></div>
                1. 本文介绍了将命名空间与从变量创建的类一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我创建了这两个类

                  //Quarter.php
                  namespace Resources;
                  class Quarter {
                      ...
                  }
                  
                  
                  //Epoch.php
                  namespace Resources;
                  class Epoch {
                  
                      public static function initFromType($value, $type) {
                          $class = "Quarter";
                          return new $class($value, $type);
                      }    
                  }
                  

                  现在这是两者的一个非常简化的版本,但足以说明我的问题.此处显示的类将不起作用,因为它找不到 Quarter 类.为了使它工作,我可以将 $class 变量更改为

                  Now this is a a very simplified version of both, but is enough to illustrate my question. The classes as they are shown here will not work as it will not find the Quarter class. To make it work I could change the $class variable to

                  $class = "ResourcesQuarter";
                  

                  所以我的问题是:当两个类已经是同一个命名空间的成员时,为什么我需要在这里使用命名空间.仅当我将类名放入变量中时才需要命名空间:

                  So my question is: Why do I need to use the namespace here when both classes are already members of the same namespace. The namespace is only needed when I put the classname in a variable so doing:

                      public static function initFromType($value, $type) {
                          return new Quarter($value, $type);
                      }    
                  

                  将毫无问题地工作.为什么会这样?这里有什么我需要避免的潜在陷阱吗?

                  will work without problems. Why is this and is there any potential traps here I need to avoid?

                  推荐答案

                  因为字符串可以从一个命名空间传递到另一个命名空间.这使得名称解析充其量是模棱两可的,并且很容易引入奇怪的问题.

                  Because strings can be passed around from one namespace to another. That makes name resolution ambiguous at best and easily introduces weird problems.

                  namespace Foo;
                  
                  $class = 'Baz';
                  
                  namespace Bar;
                  
                  new $class;  // what class will be instantiated?
                  

                  某个命名空间中的文字没有这个问题:

                  A literal in a certain namespace does not have this problem:

                  namespace Foo;
                  
                  new Baz;     // can't be moved, it's unequivocally FooBaz
                  

                  因此,所有字符串类名"都是绝对的,需要写成FQN:

                  Therefore, all "string class names" are always absolute and need to be written as FQN:

                  $class = 'FooBaz';
                  

                  (注意:没有前导.)

                  您可以将其用作简写,相当于类中的自引用 self:

                  You can use this as shorthand, sort of equivalent to a self-referential self in classes:

                  $class = __NAMESPACE__ . 'Baz';
                  

                  这篇关于将命名空间与从变量创建的类一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                  <tfoot id='U2Diq'></tfoot>

                        <i id='U2Diq'><tr id='U2Diq'><dt id='U2Diq'><q id='U2Diq'><span id='U2Diq'><b id='U2Diq'><form id='U2Diq'><ins id='U2Diq'></ins><ul id='U2Diq'></ul><sub id='U2Diq'></sub></form><legend id='U2Diq'></legend><bdo id='U2Diq'><pre id='U2Diq'><center id='U2Diq'></center></pre></bdo></b><th id='U2Diq'></th></span></q></dt></tr></i><div id='U2Diq'><tfoot id='U2Diq'></tfoot><dl id='U2Diq'><fieldset id='U2Diq'></fieldset></dl></div>
                          <tbody id='U2Diq'></tbody>
                      1. <small id='U2Diq'></small><noframes id='U2Diq'>

                      2. <legend id='U2Diq'><style id='U2Diq'><dir id='U2Diq'><q id='U2Diq'></q></dir></style></legend>
                            <bdo id='U2Diq'></bdo><ul id='U2Diq'></ul>