dongshao4207 2013-11-26 00:02
浏览 13
已采纳

无限地寻找父母的对象

I'm propably too tired but I've been wrestling with this one for a while. A container can either have a layout on its own, or it can have a parent somewhere up the hierarchy that has a layout. I need to find that layout no matter how far up the line it is.

The object $container has a property $parent which, if not null, references another $container object (same type).

I think this code has a problem in the while condition?

private function findInheritedLayout($container) {

    do {
        if ( $container->getLayoutTemplate() ) {
            return $container->getLayoutTemplate();
        }
        else {
            if ( $container->getParent() ) {
                $container = $container->getParent();
            }
            else {
                return null;
            }   
        }
    } while ( $container->getParent() ); //flawed? looks for a parent one step too far?
}
  • 写回答

2条回答 默认 最新

  • dongmijgnnq0118 2013-11-26 00:37
    关注

    Your assumption is correct. The while condition checks if the current $container has a parent, without checking for a layout.

    Try the following:

    while (!$container->getLayoutTemplate() && $container->getParent()) {
        $container = $container->getParent();
    }
    
    $template = $container->getLayoutTemplate();
    return $template ?: null;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作