我已经编写了一个php类,其中包含2个函数,但当我在不同的脚本中调用函数时,它只允许运行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);
?>
有人知道为什么会发生这种事吗?
提前感谢
这就是为什么:
他们都这样做$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->rdf,因为在我看来,没有理由这么做。
退出<==正在扼杀它
尝试从函数内部删除exit,并在调用完成后将其放入。
您的函数中有出口,因此在转到下一个函数