我编写了一个包含2个函数的php类但是当我调用不同脚本中的函数时,它只允许运行2个函数中的1个。
这是带有功能的脚本。
<?php
define('RDFAPI_INCLUDE_DIR', 'rdfapi-php/api/');
require_once('SimpleRdfParser.php');
class retrieve{
public $p;
public $uri;
public $rdf;
function retrieve(){
$this->p = new SimpleRdfParser();
$this->uri = 'rdfs/crime.owl';
$this->rdf = @file($this->uri);
}
function getName(){
return "heyyy";
}
public function getL1Comment($type){
/*
this function gets the comments that are to do with the main type of crime i.e. Sexual Offences
*/
if (is_array($this->rdf)) {
$this->rdf = join('', $this->rdf);
if (is_array($data = $this->p->string2triples($this->rdf, $this->uri))) {
$val = $data["http://localhost/".$type][2][1][0];
return $val;
exit;
}
}
}
public function getChildComment($crime){
/*
this function gets the comments from the child node of the main type of crime i.e. Rape of a Female aged 16 and over,
this is a child node of Sexual Offences
*/
if (is_array($this->rdf)) {
$this->rdf = join('', $this->rdf);
if (is_array($data = $this->p->string2triples($this->rdf, $this->uri))) {
$val = $data["http://localhost/".$crime][2][1][0];
return $val;
exit;
}
}
}
}
这是调用它的脚本:
<?php
require('retrieve.php');
$type = $_POST["type"];
$crime = $_POST["crime"];
$q = new retrieve();
echo $q->getL1Comment($type)."<br />";
//print($q->getL1Comment($type)."<br />");
print($q->getName());
//print($q->getName());
echo $q->getChildComment($crime);
?>
有没有人知道为什么会这样?
提前谢谢
答案 0 :(得分:2)
这就是原因:
他们都这样做$this->rdf = join('', $this->rdf);
他们都是有条件的:
if (is_array($this->rdf))
所以第一个导致数组不再是数组。因此,第二种方法的条件将失败。
尝试这样的事情:
public function getL1Comment($type){
/*
this function gets the comments that are to do with the main type of crime i.e. Sexual Offences
*/
if (is_array($data = $this->p->string2triples(join('', $this->rdf), $this->uri))) {
$val = $data["http://localhost/".$type][2][1][0];
return $val;
}
}
这样你就不会在方法中重新定义$ this-&gt; rdf,因为我认为没有理由这样做。
答案 1 :(得分:1)
出口; &lt; ==这就是杀了它
尝试从功能内部删除退出,并在完成通话后将其放入。
答案 2 :(得分:-1)
你已经退出了你的函数,所以它在转到下一个函数之前被转义