PHP注入类变量

时间:2015-06-18 08:11:23

标签: php

我有一个类变量

_

然后我有一个字符串

private $email;

在我的xml字符串中,我需要注入变量,而不是硬编码的电子邮件。我尝试了以下但它似乎无法正常工作

$xml = '<domDoc>';
$xml .= '<recipient xtkschema="nms:recipient" operation="insert"   email="myemail@me.com" />';
$xml .= '</domDoc>';

$params = array('sessiontoken' => $sessionToken, 'domDoc' => new SoapVar($xml, XSD_ANYXML));

我做错了吗?

由于

2 个答案:

答案 0 :(得分:1)

您忘记了email属性的字符串分隔符。您当前的代码如下所示:

$xml .= '<recipient xtkschema="nms:recipient" _operation="insert"  email=' .$this->email.' />';

假设$this->email的值为“user@example.com”,运行PHP后,您的HTML将如下所示:

$xml .= '<recipient xtkschema="nms:recipient" _operation="insert"  email=user@example.com />';

请注意,对于email属性,它缺少值周围的字符串分隔符。只需在字符串中添加双引号即可设置!

$xml .= '<recipient xtkschema="nms:recipient" _operation="insert"  email="' .$this->email.'" />';

答案 1 :(得分:0)

您应该使用:

$xml .= '<recipient xtkschema="nms:recipient" _operation="insert" email="' .$this->email.'" />';

而不是

$xml .= '<recipient xtkschema="nms:recipient" _operation="insert" email=' .$this->email.' />';