Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php静态函数运行两次_Php_Static Functions - Fatal编程技术网

php静态函数运行两次

php静态函数运行两次,php,static-functions,Php,Static Functions,我在学习php静态函数方面是新手,我想构建一个函数,从某个url中卷曲一些内容,然后处理php正则表达式以获得所需的内容。这是我的代码,但是curl部分运行了两次。如何修改它以缩短durante的运行时间 $url = 'www.php.net/archive/2012.php'; if (!$url){ exit; } echo TestClass::getTitle1($url); echo '<hr />'; echo TestClass::getTitle2($url

我在学习php静态函数方面是新手,我想构建一个函数,从某个url中卷曲一些内容,然后处理php正则表达式以获得所需的内容。这是我的代码,但是curl部分运行了两次。如何修改它以缩短durante的运行时间

$url = 'www.php.net/archive/2012.php';
if (!$url){
    exit;
}
echo TestClass::getTitle1($url);
echo '<hr />';
echo TestClass::getTitle2($url);
class TestClass
{
    static function getTitle1($url)
    {
        $data = self::getHtml($url);// run in first time
        preg_match("/(<h1.*>)(.*)(<\/h1>)/",$data,$h1tags); 
        $h1 =  $h1tags[0];
        if (!$h1) return false;
        return $h1;
    }
    static function getTitle2($url)
    {
        $data = self::getHtml($url);// run in second time
        preg_match("/(<h2.*>)(.*)(<\/h2>)/",$data,$h2tags); 
        $h2 =  $h2tags[0];
        if (!$h2) return false;
        return $h2;
    }
    static function getHtml($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $htmls = curl_exec($ch);
        curl_close($ch);
        if (!$htmls) return false;
        return $htmls;
    }
}
$url='www.php.net/archive/2012.php';
如果(!$url){
出口
}
echo TestClass::getTitle1($url);
回声“
”; echo TestClass::getTitle2($url); 类TestClass { 静态函数getTitle1($url) { $data=self::getHtml($url);//第一次运行 preg_匹配(“/()(*)()/”,$data,$h1tags); $h1=$h1标签[0]; 如果(!$h1)返回false; 返回$h1; } 静态函数getTitle2($url) { $data=self::getHtml($url);//第二次运行 preg_match(“/()(*)()/”,$data,$h2tags); $h2=$h2标签[0]; 如果(!$h2)返回false; 返回$h2; } 静态函数getHtml($url){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$URL); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $htmls=curl\u exec($ch); 卷曲关闭($ch); 如果(!$htmls)返回false; 返回$htmls; } }
将h1/h2作为参数传递给函数并重写replace函数。或者在外部运行curl并将结果传递给replace函数。

就像其他人告诉您的那样,不要重复使用参数

但是,如果您需要将来可能使用的另一个类的方法(在多个不同的函数中使用一次性调用的数据),我已经为您编辑了它

<?php
class TestClass{
    protected static $data;

    static function getTitle1($url){
        //check if $data is set or not
        $data = self::$data ? self::$data : self::getHtml($url);

        //do your code here
        preg_match("/(<h1.*>)(.*)(<\/h1>)/",$data,$h1tags); 
        $h1 =  $h1tags[0];
        if (!$h1) return false;
        return $h1;
    }
    static function getTitle2($url){
        //check if $data is set or not
        $data = self::$data ? self::$data : self::getHtml($url);

        //do your code here
        preg_match("/(<h1.*>)(.*)(<\/h1>)/",$data,$h1tags); 
        $h1 =  $h1tags[0];
        if (!$h1) return false;
        return $h1;
    }
    static function getHtml($url){        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $htmls = curl_exec($ch);
        curl_close($ch);
        if (!$htmls) return false;

        //ADD THIS LINE TO YOUR CODE AS WELL
        self::$data = $htmls;
        return $htmls;
    }
}
$url = 'www.php.net/archive/2012.php';
$class = new TestClass();
echo $class->getTitle1($url);
echo '<hr />';
echo $class->getTitle2($url);
?>


为什么不使用1方法并将
h1
/
h2
作为参数传递?不要重复-Yourself@LawrenceCherone,我想在数组中输出
h1/h2
,我想
TestClass::getTitle1($url)
TestClass::getTitle2($url)对我来说很容易控制。