【发布时间】: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