Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.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 - Fatal编程技术网

Php 让它作为一个类运行的好方法是什么

Php 让它作为一个类运行的好方法是什么,php,Php,我理解类是如何有用的,但不知道创建具有准备语句的类的正确方法是什么。我已经开始创建一个类,我想要一个名为isOnline的方法,如果url在线,它将返回 // This is the code I'm trying to make a class. global $db; $stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url"); $stmt->bind_param("i", $_SESSION['a

我理解类是如何有用的,但不知道创建具有准备语句的类的正确方法是什么。我已经开始创建一个类,我想要一个名为isOnline的方法,如果url在线,它将返回

// This is the code I'm trying to make a class.

global $db;
$stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
$stmt->bind_param("i", $_SESSION['admin_id']);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php
while($row = $result->fetch_array())
  {

    $url = $row['url'];

    $site = strtolower($url);

      // check if is online

       $curl = curl_init();
        curl_setopt_array( $curl, array(
            CURLOPT_HEADER => true,
            CURLOPT_NOBODY => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_URL => 'https://'.$site ) );
        $headers = explode( "\n", curl_exec( $curl ) );

        $statushttp = $headers[0];

        if(!$statushttp == 0){

        }else{

              $curl = curl_init();
              curl_setopt_array( $curl, array(
                  CURLOPT_HEADER => true,
                  CURLOPT_NOBODY => true,
                  CURLOPT_RETURNTRANSFER => true,
                  CURLOPT_URL => 'http://'.$site ) );
              $headers = explode( "\n", curl_exec( $curl ) );

              $statushttp = $headers[0];

        }


        echo "$url with $statushttp <br>";

        // email person here.



  }
$stmt->free_result();

不确定你在做什么来确定一个站点是否在线,但要创建一个类来处理这个问题。你应该用这样的东西。如果你能解释你正在做什么来确定一个网站是否正常,我可以更新这个类

class SomeClassNameHere{

    //this is the constructor of the class.  This is ran when you create it
    public SomeClassNameHere(){

    }

    //this is a public function that you can call 
    public function isOnline(){
        //this is the first step to breaking up this into smaller peaces
        //I moved your section of code that gets the urls from the database
        //into a function that returns the rows from the database below.
        $rows = getUrls();
        while($row = $result->fetch_array()){
            $site = strtolower($row['url']);
        }
        // check if is online
        $curl = curl_init();
        curl_setopt_array( $curl, array(
        CURLOPT_HEADER => true,
        CURLOPT_NOBODY => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => 'https://'.$site ) );
        $headers = explode( "\n", curl_exec( $curl ) );
        $statushttp = $headers[0];

        if(!$statushttp == 0){

        }else{

          $curl = curl_init();
          curl_setopt_array( $curl, array(
              CURLOPT_HEADER => true,
              CURLOPT_NOBODY => true,
              CURLOPT_RETURNTRANSFER => true,
              CURLOPT_URL => 'http://'.$site ) );
          $headers = explode( "\n", curl_exec( $curl ) );

          $statushttp = $headers[0];

        }
        //I wouldn't echo out the results here, 
        //I would have this function return a true or false and do the display
        //somewhere else

        //like this
        if(statushttp == whateverIsTrue){   //remember to edit this
            return true;
        }else{
            return false;
        }
    }

    //this has the possibility to return more than one row.  You will need to
    //change your function to handle this.
    public function getUrls(){
        $stmt = $db->prepare("SELECT url FROM urls WHERE rel=? ORDER BY url");
        $stmt->bind_param("i", $_SESSION['admin_id']);
        $stmt->execute();
        return $stmt->get_result();
    }
}
要调用这个类,您需要执行类似的操作

//this is creating an instance of the class
$foo= new SomeClassNameHere();
//this will call the isOnline function of that class and return true or false
$siteOnline = $foo->isOnline();
if($siteOnline){
    echo "Yeah!";
}else{
    echo "Sad:-(";
}

我正在连接到特定用户的url数据库,然后使用curl获取标题响应。好的,我已经更新了答案,以帮助您找到更好的方向。类的要点是将事物分解成类/对象/模型/服务。考虑对象而不仅仅是代码。我已经测试过了,但没有发现任何错误或返回任何东西。我做了这个$u=新扫描仪21$u->getURL;echo$u->isOnline@DrewPeer我已经更新了答案,展示了如何调用类和函数。我还在原始函数中添加了注释,说明了为了使类正常工作,您需要更改哪些内容。感谢您的响应和编辑-未捕获错误:我得到了对未定义函数getUrls的调用。我编辑getUrls方法。