【问题标题】:Call php function from different class, namespace and file using include使用包含从不同的类、命名空间和文件调用 php 函数
【发布时间】:2017-12-11 20:56:25
【问题描述】:

我知道这个问题与其他几个问题非常相似,但我似乎在遵循这些建议之后调用该函数也有问题。感谢您的理解。

我正在尝试使用此文件中的“setUserName”函数设置用户名从另一个文件调用该函数,如下所示。我似乎误解了如何调用类和命名空间。

namespace StructType;

use \WsdlToPhp\PackageBase\AbstractStructBase;

/**
 * This class stands for webExport StructType
 * @subpackage Structs
 */
class WebExport extends AbstractStructBase
{
    /**
     * The userName
     * Meta informations extracted from the WSDL
     * - nillable: true
     * @var string
     */
    public $userName;
    /**
     * The passwordMd5
     * Meta informations extracted from the WSDL
     * - nillable: true
     * @var string
     */
    public $passwordMd5;
    /**
     * The ecfFile
     * Meta informations extracted from the WSDL
     * - nillable: true
     * @var string
     */
    public $ecfFile;
    /**
     * Constructor method for webExport
     * @uses WebExport::setUserName()
     * @uses WebExport::setPasswordMd5()
     * @uses WebExport::setEcfFile()
     * @param string $userName
     * @param string $passwordMd5
     * @param string $ecfFile
     */
    public function __construct($userName = null, $passwordMd5 = null, $ecfFile = null)
    {
        $this
            ->setUserName($userName)
            ->setPasswordMd5($passwordMd5)
            ->setEcfFile($ecfFile);
    }
    /**
     * Get userName value
     * @return string|null
     */
    public function getUserName()
    {
        return $this->userName;
    }
    /**
     * Set userName value
     * @param string $userName
     * @return \StructType\WebExport
     */
    public function setUserName($userName = null)
    {
        // validation for constraint: string
        if (!is_null($userName) && !is_string($userName)) {
            throw new \InvalidArgumentException(sprintf('Invalid value, please provide a string, "%s" given', gettype($userName)), __LINE__);
        }
        $this->userName = $userName;
        return $this;
    }
    /**
     * Get passwordMd5 value
     * @return string|null
     */
    public function getPasswordMd5()
    {
        return $this->passwordMd5;
    }
    /**
     * Set passwordMd5 value
     * @param string $passwordMd5
     * @return \StructType\WebExport
     */
    public function setPasswordMd5($passwordMd5 = null)
    {
        // validation for constraint: string
        if (!is_null($passwordMd5) && !is_string($passwordMd5)) {
            throw new \InvalidArgumentException(sprintf('Invalid value, please provide a string, "%s" given', gettype($passwordMd5)), __LINE__);
        }
        $this->passwordMd5 = $passwordMd5;
        return $this;
    }
    /**
     * Get ecfFile value
     * @return string|null
     */
    public function getEcfFile()
    {
        return $this->ecfFile;
    }
    /**
     * Set ecfFile value
     * @param string $ecfFile
     * @return \StructType\WebExport
     */
    public function setEcfFile($ecfFile = null)
    {
        // validation for constraint: string
        if (!is_null($ecfFile) && !is_string($ecfFile)) {
            throw new \InvalidArgumentException(sprintf('Invalid value, please provide a string, "%s" given', gettype($ecfFile)), __LINE__);
        }
        $this->ecfFile = $ecfFile;
        return $this;
    }
    /**
     * Method called when an object has been exported with var_export() functions
     * It allows to return an object instantiated with the values
     * @see AbstractStructBase::__set_state()
     * @uses AbstractStructBase::__set_state()
     * @param array $array the exported values
     * @return \StructType\WebExport
     */
    public static function __set_state(array $array)
    {
        return parent::__set_state($array);
    }
    /**
     * Method returning the class name
     * @return string __CLASS__
     */
    public function __toString()
    {
        return __CLASS__;
    }
}

这是我如何尝试设置用户名从另一个文件调用函数。

<?php

require_once __DIR__ . '/vendor/autoload.php';

    class Class2{
        use \StructType\webExport;
        public static function staticFunction(){
            setUserName("webservice");
        }
    }

我哪里出错了?如果我直接在构造中设置密码,一切都会按预期工作:

 public function __construct($userName = "ws", $passwordMd5 = "qwerty", $ecfFile = null)

最后一条错误信息:

致命错误:Class2 无法使用 StructType\WebExport - 它不是第 289 行 index.php 中的特征

【问题讨论】:

  • 1) use 语句应该在类定义之前。 2) 拥有 use 语句并不意味着您不实例化对象 3) 需要在该实例化对象上调用该方法。
  • 所以你的意思是我应该在类中这样称呼它:\StructType\webExport\setUserName("webservice"); ?按照你的建议做让我更进一步,因为我没有收到任何错误,但用户名似乎仍然没有设置。
  • 你知道如何使用基本的 PHP 对象吗?
  • 是的,我知道如何使用基本的 PHP 对象。但是我仍然被困在一个我自己无法解决的问题上。

标签: php function namespaces


【解决方案1】:

在类中使用use,表示使用Traits,如果要创建WebExport的对象,可以使用关键字new创建WebExport的对象,例如:

public static function staticFunction() {
    $webExport = new \StructType\WebExport();
    $webExport->setUserName('webservice');
    $webExport->setPasswordMd5('somemd5password');
    $webExport->setEcfFile(somefile)'
}

特征:http://php.net/manual/en/language.oop5.traits.php

【讨论】:

    【解决方案2】:

    所以,这里有几件事...... 当你有一个use 声明inside 一个类的定义,它会期待一个Trait。该 use 语句确实需要在类的定义之外,因为您在这里使用的是 Class 而不是 Trait。此外,当您想使用一个类时,case 很重要(可能只是 OP 问题中的 type-o)。 WebExport 不是 webExport。最后,如果您要设置类的实例变量,则需要有一个 该类的实例 才能设置它的成员/实例变量之一。我不确定您的 Class2 是否打算扩展 WebExport 类,或者您是否想使用组合并在新 Class2 的 WebExport property 上设置用户名。 (我不确定你打算如何组织你的课程),但我希望有几种可能的用途中的任何一种......

    require_once __DIR__ . '/vendor/autoload.php';
    use \StructType\WebExport;
    
    class Class2 {
    
       protected $webExportMemberVariable;
    
       public function __construct() {
           $this->webExportMemberVariable = new WebExport();
       }
    
       public function noLongerStaticFunction() {
          $this->webExportMemberVariable->setUserName("webservice");
       }
    }
    

    ^ 请注意,WebExport 在 use 语句中具有大写字母“W”,我在 WebExport 类型的 实例 上调用 setUserName。以静态方式执行此操作似乎有点奇怪,但仍有可能......

    require_once __DIR__ . '/vendor/autoload.php';
    use \StructType\WebExport;
    
    class Class2 {
    
       protected static $webExportStaticVariable = new WebExport();
    
       public static function staticFunction() {
          self::$webExportMemberVariable->setUserName("webservice");
       }
    }
    

    ^ 从静态方法执行此操作有点不可靠恕我直言。在任何情况下,您必须拥有一个 \StructType\WebExport 类型的实例来设置一个成员变量,就像您在这里想要做的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 2019-06-20
      相关资源
      最近更新 更多