65

I am getting an error saying

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; TSStatus has a deprecated constructor in C:\Program Files (x86)\Zend\Apache24\htdocs\viewer\modules\tsstatus\tsstatus.php on line 10

class TSStatus is line 10 plus at the bottom TSStatus shows

class TSStatus
{
    private $_host;
    private $_queryPort;
    private $_serverDatas;
    private $_channelDatas;
    private $_userDatas;
    private $_serverGroupFlags;
    private $_channelGroupFlags;
    private $_login;
    private $_password;
    private $_cacheFile;
    private $_cacheTime;
    private $_channelList;
    private $_useCommand;
    private $_javascriptName;
    private $_socket;

    public $imagePath;
    public $showNicknameBox;
    public $timeout;
    public $hideEmptyChannels;
    public $hideParentChannels;

    public function TSStatus($host, $queryPort)

    ...
}
4
  • 7
    So use the proper public function __construct($host, $queryPort) constructor in your code, as the PHP Documentation tells you to do
    – Mark Baker
    May 8, 2016 at 13:31
  • Or public static self::. But mark is right. Construct is better.
    – cpugourou
    May 8, 2016 at 13:33
  • 1
    Don't use PHP 4 style constructors. From the documentation, PHP 4 style constructors (methods that have the same name as the class they are defined in) are deprecated, and will be removed in the future. May 8, 2016 at 13:37
  • Using FPDF with the file "pdf_parser.php". Line 101 has "function pdf_parser($filename)". Changing this to "function __construct($filename)" and it no longer works. Any ideas anyone?
    – Cymro
    Jul 11, 2020 at 19:33

2 Answers 2

184

As mentioned in the error, the official manual and the comments:

Replace

public function TSStatus($host, $queryPort)

with

public function __construct($host, $queryPort)
6
  • 7
    Because I was wondering: Yes, it's a drop-in-replacement. Nothing else to do but just rename it. Apr 3, 2018 at 23:54
  • 1
    Will the functionality got disturbed if I do that change? Jun 29, 2018 at 7:31
  • 2
    @AminahNuraini No (see first comment) Jun 29, 2018 at 15:18
  • Thanks! Indeed it's still working on PHP 7.4, it just prints deprecated message. I tried __construct on PHP5 and it alrdy works, so it's all good and fun.
    – Hikari
    Oct 2, 2019 at 13:23
  • 4
    You failed to mention that calls to the constructor need to be changed to "parent::__construct();"
    – Cymro
    Jul 11, 2020 at 20:09
0

I was getting warnings, about this problem, and now it start to work. At first it was:

function Smarty()
{
  $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
                : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);

But I made this way:

function _construct()
    {
      $this->assign('SCRIPT_NAME', isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME']
                    : @$GLOBALS['HTTP_SERVER_VARS']['SCRIPT_NAME']);

Now I've stoped getting warnings and the script is working well.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Feb 22, 2022 at 21:12