繁体   English   中英

将PHP 5.6新闻脚本转换为7.2

[英]Convert PHP 5.6 news script to 7.2

我有一个PHP 5.6兼容脚本。 现在在我的服务器上,我必须更改为php7.2,但是此脚本到处都出错。我想自己进行转换,但是我的知识还不够多。 我一直在通过一些教程一点一点地前进,但是我已经陷入困境而无法继续。

我自己需要帮助。 这只是代码的一个示例,但是通过解决这个问题,我认为我将能够转换整个网站。

错误:

   [01-Nov-2018 09:16:58 UTC] PHP Fatal error:  Uncaught Error: Call to a member function query() on null in /home/xxxx/public_html/incs/generales/fullnews.php:433
Stack trace:
#0 /home/xxxx/public_html/news.php(16): FullNewsInc->getFullNews('18880', 'fullnews')
#1 {main}
  thrown in /home/xxxx/public_html/incs/generales/fullnews.php on line 433

news.php页面

  <?php   
require_once './db.php'; $db = new DB_Sql(); $db2 = new DB_Sql();  
include_once './fullnews.php';
$fullnewsinc = new FullNewsInc($db); 
$newsId = $_REQUEST['newsId'];
$system = $_REQUEST['system'];
$cat = $_REQUEST['categorie']; 

?> 
    <section id="main-content" itemscope itemtype="https://schema.org/Article">  
        <div id="latest">
            <section class="noticias"> 
<div class="clearfix"></div>   
 <?php $fullnewsinc->getFullNews($newsId, 'fullnews'); ?>  

            </section>   

            <aside class="sidebar">  
                        </aside>
        </div> <!-- END Latest -->
        <div class="clearfix"></div> 
    </section>   

这是fullnews.php中的功能

<?php

class FullNewsInc {

    var $db;
 public function __construct()
    {
        // Constructor's functionality here, if you have any.
    }

    public function FullNewsInc(&$db) {

        $this->db = &$db;  self::__construct();

    } 

    function getFullNews($newsId = '', $template = 'theme_fullnews') {

        $sql = "SELECT x FROM";

        $this->db->query($sql);

        while($this->db->next_record()) {
            $newsId = $this->db->f("newsId");
            $subject = $this->db->f("subject");
            $shortNews = $this->db->f("shortNews");
            $longNews = $this->db->f("longNews");
            $iconId = $this->db->f("iconId");
            $source = $this->db->f("source");
            include "./theme/".$template.".tpl.php";

        }

    }


}?>

这是db.php

<?php 
if (!class_exists('DB_Sql')){  
class DB_Sql { 

  public function __construct()
    {
        // Constructor's public functionality here, if you have any.
    }
     public $Host     = "xxx";
  public $Database = "xxxx";
  public $User     = "xxx";
  public $Password = "xxxxx";
  public $Auto_Free     = 1;
  public $Debug         = 0;
  public $Halt_On_Error = "no"; // "yes", "no", "report"
  public $Record   = array();
  public $Row;
  public $Errno    = 0;
  public $Error    = "";
  public $Link_ID  = 0;
  public $Query_ID = 0;

  public function DB_Sql($query = "") {
      $this->query($query);self::__construct();
  }

  public function link_id() {
    return $this->Link_ID;self::__construct();
  }

  public function query_id() {
    return $this->Query_ID;self::__construct();
  }

  public function connect($Database = "", $Host = "", $User = "", $Password = "") {
    if ("" == $Database)
      $Database = $this->Database;
    if ("" == $Host)
      $Host     = $this->Host;
    if ("" == $User)
      $User     = $this->User;
    if ("" == $Password)
      $Password = $this->Password;

    if ( 0 == $this->Link_ID ) {
      $this->Link_ID=@mysqli_connect($Host, $User, $Password, $Database);
      if (!$this->Link_ID) {
        $this->halt("connect failed - Please check your database settings.");
        die;
      }

      if (!@mysqli_select_db($Database,$this->Link_ID)) {
        $this->halt("cannot use database ".$this->Database." - Please check your database settings.");
        die;
      }
    }

    return $this->Link_ID;
  }

  public function free() {
      @mysql_free_result($this->Query_ID);
      $this->Query_ID = 0;self::__construct();
  }

  public function query($Query_String) {
    if ($Query_String == "") {
      return 0;
    }

    if (!$this->connect()) {
      return 0;
    }

    # New query, discard previous result.
    if ($this->Query_ID) {
      $this->free();
    }

    if ($this->Debug)
      printf("Debug: query = %s<br />\n", $Query_String);

    $this->Query_ID = @mysqli_connect($Query_String,$this->Link_ID);
    $this->Row   = 0;
    $this->Errno = mysql_errno();
    $this->Error = mysql_error();
    if (!$this->Query_ID) {
      $this->halt("Invalid SQL: ".$Query_String);
    }

    return $this->Query_ID;self::__construct();
  }

  public function next_record() {
    if (!$this->Query_ID) {
      $this->halt("next_record called with no query pending.");
      return 0;
    }

    $this->Record = @mysqli_fetch_array($this->Query_ID);
    $this->Row   += 1;
    $this->Errno  = mysql_errno();
    $this->Error  = mysql_error();

    $stat = is_array($this->Record);
    if (!$stat && $this->Auto_Free) {
      $this->free();
    }
    return $stat;self::__construct();
  }

  public function affected_rows() {
    return @mysql_affected_rows($this->Link_ID);self::__construct();
  }

  public function num_rows() {
    return @mysql_num_rows($this->Query_ID);self::__construct();
  }

  public function num_fields() {
    return @mysql_num_fields($this->Query_ID);self::__construct();
  }

  public function nf() {
    return $this->num_rows();self::__construct();
  }

  public function np() {
    print $this->num_rows();self::__construct();
  }

  public function f($Name) {
    return $this->Record[$Name];self::__construct();
  }

  public function p($Name) {
    print $this->Record[$Name];self::__construct();
  }

  public function halt($msg) {
    $this->Error = @mysqli_error($this->Link_ID);
    $this->Errno = @mysqli_errno($this->Link_ID);
    if ($this->Halt_On_Error == "no")
      return;

    $this->haltmsg($msg);

    if ($this->Halt_On_Error != "report")
      die("Session halted.");self::__construct();
  }

  public function haltmsg($msg) {
    printf("<b>Database error:</b> %s<br />\n", $msg);
    printf("<b>MySQL Error</b>: %s (%s)<br />\n",
      $this->Errno,
      $this->Error);self::__construct();
  }
}

}

theme_fullnews模板:

<article>
<header>   
  <h1 itemprop="name" ><?php echo $subject; ?></h1> 
  <h2 itemprop="articleSection"><?php echo $shortNews; ?></h2>

  <span itemprop="datePublished" content=" <?php echo $newDate; ?>">  <?php   echo time_elapsed_string($timestamp);   ?> </span>


    </div>
  </header>

<section>   


    <?php $longNews); ?>


</section>

</article>

这里发生的事情是您的类中具有与该类同名的函数。 PHP向您发出警告,将来会删除此功能。

例如,您有php类

<?php

class FullNewsInc {
    public function __construct( )
    {
        // Constructor's public functionality here, if you have any.

    }

    /**
        Some code here
    **/

    public function FullNewsInc($db){
        $this->db = &$db;
    }

    /**
        More code here
    **/
}

类和方法的名称匹配,因此您需要更改PHP类以使用您已经编写的__construct方法,但是当前为空。

class FullNewsInc {
    public function __construct( $db )
    {
        // Constructor's public functionality here, if you have any.
        $this->db = &$db;
    }

    /**
      Some code here
    **/

    //The FullNewsInc method has been deleted. The code contained within was moved to the __construct

    /**
      More code here
    **/
}

php.net网站http://php.net/manual/en/language.oop5.decon.php上有更多信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM