Deprecated: Methods with the same name as their class will not be constructors in a future version

该博客介绍了PHP代码中关于类构造方法的弃用警告,PHP7.0及更高版本不再支持与类名相同的构造方法,需改为`__construct()`。示例代码展示了如何创建和操作树形结构数据,包括设置节点、获取子节点、父节点及层级遍历。修复后的代码能正确运行并输出树形结构。
摘要由CSDN通过智能技术生成

问题描述:

Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Tree has a deprecated constructor in D:\software\phpEnv\www\localhost\Tree.php on line 2

php代码:

<?php
class Tree
{
    public $data=array();
    public $cateArray=array();
    public $res=array();
    function Tree()
    {
    }
    function setNode ($id, $parent, $value)
    {
        $parent = $parent?$parent:0;
        $this->data[$id] = $value;
        //print_r($this->data);
        //echo "\r\n";
        $this->cateArray[$id] = $parent; //节点数组
        //print_r($this->cateArray);
    }
    function getChildsTree($id=0)
    {
        $childs=array();
        foreach ($this->cateArray as $child=>$parent)
        {
            if ($parent==$id)
            {
                $childs[$child]=$this->getChildsTree($child);
            }
        }
        print_r($childs)."/r/n";
        return $childs;
    }
    function getParentsTree($id=0)
    {
        $parents=array();
        foreach ($this->cateArray as $child=>$parent)
        {
            if ($child ==$id)
            {
                $parents[$parent]=$this->getParentsTree($parent);
            }
        }
        print_r($parents)."/r/n";
        return $parents;
    }
    function getChilds($id=0)
    {
        $childArray=array();
        $childs=$this->getChild($id);
        foreach ($childs as $child)
        {
            $childArray[]=$child;
            $childArray=array_merge($childArray,$this->getChilds($child));
        }
        return $childArray;
    }
    function getChild($id)
    {
        $childs=array();
        foreach ($this->cateArray as $child=>$parent)
        {
            if ($parent==$id)
            {
                $childs[$child]=$child;
            }
        }
        return $childs;
    }
    function getParents($id)
    {
        $parentArray=array();
        $parents=$this->getParent($id);
        foreach ($parents as $parent)
        {
            $parentArray[]=$parent;
            $parentArray=array_merge($parentArray,$this->getParents($parent));
        }
        return $parentArray;
    }
    function getParent($id)
    {
        $parents=array();
        foreach ($this->cateArray as $child=>$parent)
        {
            if ($child==$id)
            {
                $parents[$parent]=$parent;
            }
        }
        return $parents;
    }
    //单线获取父节点
    function getNodeLever($id)
    {
        $parents=array();
        if (key_exists($this->cateArray[$id],$this->cateArray))
        {
            $parents[]=$this->cateArray[$id];
            $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
        }
        return $parents;
    }
    function getLayer($id,$preStr='|-')
    {
        return str_repeat($preStr,count($this->getNodeLever($id)));
    }
    function getValue ($id)
    {
        return $this->data[$id];
    } // end func
    //获取所有节点数据生成树
    function getAll($id=0,$str="|-"){
        if($id!=0){
            $str=$str."|-";
        }
        //遍历所有数组检查parent是否有id
        foreach($this->cateArray as $child=>$parent){
            //检查是否有相等的ID
            if($parent==$id){
                $this->res[$child]=$str.$this->getValue($child);
                $this->getAll($child,$str);
            }
            //$this->res[$child]=$child.$str.$this->getValue($child);
        }
    }
}
//构造树
//setNode ($id, $parent, $value)
$tree = new Tree();
$tree->setNode("1","","电器");
$tree->setNode("2","","服装");
$tree->setNode("3","1","电脑");
$tree->setNode("4","1","家电");
$tree->setNode("5","2","男装");
$tree->setNode("6","2","女装");
$tree->setNode("7","3","笔记本");
$tree->setNode("8","3","台式机");
$tree->setNode("9","7","惠普");
$tree->setNode("10","7","戴尔");
$tree->setNode("11","8","火星人");
$tree->setNode("12","5","西装");
$tree->setNode("13","6","上衣");
$tree->setNode("14","9","惠普-N90");
$tree->setNode("15","9","惠普-N91");
$tree->setNode("16","10","戴尔A11");
$tree->setNode("17","10","戴尔A12");
$tree->setNode("18","10","戴尔A13");
$tree->setNode("19","6","裤子 ");
$tree->setNode("20","13","长袖");
$tree->setNode("21","13","短袖");
$tree->setNode("22","20","NIKE长袖");
$tree->setNode("23","20","361长袖");
$tree->setNode("24","22","NIKE长袖-均码");
$tree->setNode("25","22","NIKE长袖-短码");
$tree->setNode("26","14","惠普-N90-14寸");
$tree->setNode("27","14","惠普-N90-15寸");
$tree->setNode("28","14","惠普-N90-17寸");
$tree->setNode("29","28","惠普-N90-17寸-高性能企业版");
$tree->setNode("30","28","惠普-N90-17寸-普通家用版");
$tree->getAll();
foreach($tree->res as $val){
    echo $val."<br>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>生成Tree</title>
</head>
<body>
<h5>生成标签测试</5>
<select>
    <?php
    foreach($tree->res as $key=>$val){
        echo "<option value='{$key}'>{$val}</option>";
    }
    ?>
</select>
</body>
</html>

原因分析:

php7.0之后将不再支持与类名相同的构造方法,构造方法统一使用 __construct()。


解决方案:

function Tree(){}

修改为:

 function __construct(){}

运行效果:
在这里插入图片描述

参考文章链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里逆天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值