我收到致命错误:在不在对象上下文中时使用$ this

时间:2014-11-19 10:11:22

标签: php

您好我使用下面的代码,我知道在我的网站上的另一个页面中工作,但是为了这个目的,我把它放在一个独立的按钮,运行脚本作为服务器端,它不工作,并得到了致命错误:在不在对象上下文中时使用$ this

($$ postcode = $ params [" value1"];是以前的javascript从添加屏幕获取当前字段(邮政编码)的结果,它可以工作并进行测试,但是它的其余的代码。

我很感激一些替代方案吗?

$postcode= $params["value1"];

//Set Post Code Variables
$Key = "xxxx-xxxx-xxxx-xxxx";
$SearchTerm = $postcode;
$this->Key = $Key;
$this->SearchTerm = $SearchTerm;


//Build URL Request
$url = "http://services.postcodeanywhere.co.uk/PostcodeAnywhere/Interactive/Find/v1.10/xmla.ws?";
$url .= "&Key=" . urlencode($this->Key);
$url .= "&SearchTerm=" . urlencode($this->SearchTerm);

//Make the request to Postcode Anywhere and parse the XML returned
$file = simplexml_load_file($url);

//Check for an error, if there is one then throw an exception
if ($file->Columns->Column->attributes()->Name == "Error") 
{
throw new Exception("[ID] " . $file->Rows->Row->attributes()->Error . " [DESCRIPTION] " . $file->Rows->Row->attributes()->Description . " [CAUSE] " . $file->Rows->Row->attributes()->Cause . " [RESOLUTION] " . $file->Rows->Row->attributes()->Resolution);
}

//Copy the data
if ( !empty($file->Rows) )
{
foreach ($file->Rows->Row as $item)
{
$this->Data[] = array('Id'=>$item->attributes()->Id,'StreetAddress'=>$item->attributes()->StreetAddress,'Place'=>$item->attributes()->Place);

$sql = "insert into tempaddress (id, StreetAddress, Place, PostCode) values ('".$item["Id"]."', '".$item["StreetAddress"]."', '".$item["Place"]."','$SearchTerm')";  CustomQuery($sql);

}}
$result["postcoderesult"]=$postcode;

1 个答案:

答案 0 :(得分:0)

使用$ this->你必须进行类和OOP编程。你没有任何课程可以使用($ this) 如果你在一个类中有Key属性,你必须使用

从你的类中创建一个实例
$OBJ = new class;

并使用

等属性
$OBJ->Key 

$仅在您必须在课堂中调用方法或属性时使用 可能你有一个具有Key属性的类,如果你必须访问这个peropery你有你的类中的ro make实例