![](/img/trans.png)
[英]PHP - Call to undefined function requiring a file from a different folder and using namespace
[英]Call php function from different class, namespace and file using include
我知道,这个问题与其他几个问题并不完全相同,但是非常相似,但是即使遵循了这些建议,我似乎也无法调用该函数。 感谢您的理解。
我正在尝试通过此文件中的“ 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中,它不是特征
您使用一个use
内部类,表示使用Traits
,如果您想创建WebExport
的对象, WebExport
可以使用关键字new
来创建WebExport
的对象,例如:
public static function staticFunction() {
$webExport = new \StructType\WebExport();
$webExport->setUserName('webservice');
$webExport->setPasswordMd5('somemd5password');
$webExport->setEcfFile(somefile)'
}
因此,这里有两件事...当您在类的定义中包含use
语句时,它将期望使用Trait
。 该use语句确实需要在类的定义之外,因为您使用的是Class而不是Trait。 另外,当您要使用类时, 大小写很重要 (在OP的问题中可能只是type-o)。 WebExport
不是webExport
。 最后,如果要设置类的实例变量,则需要具有该类的实例才能设置其成员/实例变量之一。 我不确定您的Class2是否打算扩展WebExport类,或者您是否要使用合成并在新Class2的WebExport 属性上设置userName。 (我不确定您打算如何构造您的类),但是我希望可以使用几种可能的任何一种方法...
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语句中使用大写WebExport
“ W”,并且我在setUserName
类型的实例上调用WebExport
。 静态地执行此操作似乎有些奇怪,但仍然可以...
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
类型的实例来设置成员变量,就像您想要在此处执行的那样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.