php 不推荐通过引用分配 new 的返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:10:04  来源:igfitidea点击:

Assigning the return value of new by reference is deprecated in

phpreference

提问by Darklogix

I'm new to PHP and Im playing around with some code to see what i can learn/do. I have a simple little project where I am connecting to a mysql db and displaying the records of a table.

我是 PHP 的新手,我在玩一些代码来看看我可以学习/做什么。我有一个简单的小项目,我在其中连接到 mysql 数据库并显示表的记录。

When I put all my code in one file, everything runs smooth, as soon as I try to put a chunk of code in a separte PHP file and use an include or require_once I get the error Assigning the return value of new by reference is deprecated in...PEAR\config.php yadda yadda

当我将所有代码放在一个文件中时,一切都运行顺利,只要我尝试将一大块代码放在一个单独的 PHP 文件中并使用 include 或 require_once,我就会收到错误通过引用分配 new 的返回值已弃用在...梨\config.php yadda yadda

I've done some research and I understand that passing an object by reference (&obj) is no longer acceptable in PHP 5, that's fine, but no where in my code do I have an ampersand, and again, it all works fine if it's all in the same .php file. So i figure i might be doing something wrong with my includes or something.

我已经做了一些研究,我知道在 PHP 5 中不再接受通过引用 (&obj) 传递对象,这很好,但是我的代码中没有与号的位置,而且,如果它是,则一切正常都在同一个 .php 文件中。所以我想我可能在我的包含或其他东西上做错了什么。

This is the file that 'does the work' that i am calling from my main page with a require_once

这是我使用 require_once 从主页调用的“完成工作”的文件

include '/config.php';
// Open the database connection

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

        //Define and run the query
        $query = "SELECT * FROM wp_posts";
    $result = mysql_query($query);

    echo mysql_error();

        //Close the db connection
    mysql_close();

    echo "<table><tr><td>Post Title</td><td>Post Status</td><td>Post Date</td></tr>";

        //Loop through result set and display data

        while($tdata=mysql_fetch_array($result))
        {
            echo "<tr><td>$tdata[post_title]</td><td> $tdata[post_status]</td><td> $tdata[post_date]</td></tr>";     // name class and mark will be printed with one line break
    }

        echo "</table>";

Anyways, I know my code isn't beautiful, Im still trying to figure it all out. Thanks for any help you can provide.

无论如何,我知道我的代码并不漂亮,我仍在努力弄清楚。感谢您的任何帮助,您可以提供。

Additional Information: Ok, I know I'm a complete noob on this include stuff, and i apologize. Cant quite wrap my head around it all yet. But here's some additional info. Here is the full text of the error message I'm receving (NOTE: My code still runs and my table DOES still display)

附加信息:好的,我知道我对这方面的内容完全是个菜鸟,我深表歉意。还不能完全理解这一切。但这里有一些额外的信息。这是我收到的错误消息的全文(注意:我的代码仍在运行,我的表仍然显示)

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 80

已弃用:在 C:\xampp\php\PEAR\Config.php 中第 80 行不推荐通过引用分配 new 的返回值

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config.php on line 166

已弃用:在 C:\xampp\php\PEAR\Config.php 中第 166 行不推荐通过引用分配 new 的返回值

Deprecated: Assigning the return value of new by reference is deprecated in C:\xampp\php\PEAR\Config\Container.php on line 111

已弃用:在 C:\xampp\php\PEAR\Config\Container.php 中第 111 行不推荐通过引用分配 new 的返回值

Again, I'm using netBeans as my IDE, so in my Netbeans i went to Tools-Options and went to PHP and set C:\xampp\php\PEAR as the Global include path (Whatever the heck that means, but nonetheless it seemed like a good idea).

同样,我使用 netBeans 作为我的 IDE,所以在我的 Netbeans 中,我转到 Tools-Options 并转到 PHP 并将 C:\xampp\php\PEAR 设置为全局包含路径(不管这意味着什么,但无论如何它似乎是个好主意)。

I guess if my include path is set right (/xampp/php...) then why am I still receiving the error. Grrr. Thanks for all your help guys.

我想如果我的包含路径设置正确(/xampp/php ...),那么为什么我仍然收到错误。咕噜噜。感谢您的所有帮助家伙。

回答by GordonM

It's the third party library you're using that's causing the problem, it's out of date and is using such things as returning by reference which are no longer considered good practice. You should upgrade the library in question. From what you've written I'm guessing it's this library. http://pear.php.net/package/Config/redirected

这是您正在使用的第三方库导致了问题,它已经过时并且正在使用诸如通过引用返回之类的东西,这些东西不再被认为是好的做法。您应该升级有问题的库。从你写的内容来看,我猜是这个库。 http://pear.php.net/package/Config/redirected

Try running pear upgrade configfrom the shell.

尝试pear upgrade config从 shell运行。

回答by Richard Tuin

Your include path might be set in the wrong order.

您的包含路径可能以错误的顺序设置。

The script seems to prefer the PEAR include directory over the current working directory. There are several ways to fix this:

该脚本似乎更喜欢 PEAR 包含目录而不是当前工作目录。有几种方法可以解决这个问题:

1. use absolute includes

1. 使用绝对包含

For instance, in your case:

例如,在您的情况下:

include dirname(__FILE__) . 'config.php'; // pre-PHP 5.3
include __DIR__ . 'config.php'; // PHP 5.3

2. change the include path

2.更改包含路径

To read more about this, see this pagein the php.net documentation

要阅读有关此内容的更多信息,请参阅php.net 文档中的此页面

回答by jeckx2

Open your config.php and go to line 89 and remove the &after =

打开你的 config.php 并转到第 89 行并删除&after=

from:

从:

"$this->container =& new Config_Container('section', 'root');"

to:

到:

"$this->container = new Config_Container('section', 'root');"

Same goes to line 166.

第 166 行也是如此。

回答by Abadis

I had similar problem.The solution is change name of your file. config.php gets a file in pear folder not your config file.

我有类似的问题。解决方案是更改文件名。config.php 在 pear 文件夹中获取一个文件,而不是您的配置文件。

回答by rahul_pratap

it is simply pear package error which describes the problem about the assignment. so we can normally replace the assignment of =& with =in the given line of the error statement.that will solve our problem.

它只是梨包错误,它描述了有关分配的问题。所以我们通常可以 在错误语句的给定行中用 =替换=&的赋值 。这将解决我们的问题。

回答by Mpuppetier

Just run C:\xampp\php\pear-update.bat and restart the Apache server (On Windows macchine). For linux macchine follow the instuctions of GordonM.

只需运行 C:\xampp\php\pear-update.bat 并重新启动 Apache 服务器(在 Windows 机器上)。对于 linux 机器,请遵循 GordonM 的指示。