donglie7268 2011-10-27 18:49 采纳率: 100%
浏览 65
已采纳

使用magic __get方法时,禁止显示未识别的属性警告

I've got a CI Library which loads translatable content from an XML file into a class, and the class has a magic __get method which checks if that property is defined when it is referenced, returning the localised key if so, or the keyname plus '_#' to let me know that translation is missing if not.

All cool.

However, because this all depends on properties on an object, I get lots of 'notice: undefined etc..' warnings when I'm running in debug (E_ALL), and it's annoying. I don't want to disable notices, but i do want to know how to disable this inside this particular library (if possible). I could put @ in front of every call to the class, but again, that's pretty horrible too.

Any tips?

Simplified code snippets below:

class MY_Translation
{

    function _get_keys($lang) {
        // load xml translations, could split this into different files..

        $translations = new DOMDocument();
        $translations->load($_SERVER['DOCUMENT_ROOT']."/xml/translations.xml");
        if ($translations) {
            $words = $translations->getElementsByTagName("word");
            $count = 0;
            foreach( $words as $word ){

                $name = $word->getAttribute('name');
                $trans = $word->childNodes;

                if ($trans->length > 0) {
                    for($i = 0; $i < $trans->length; $i++) {
                        $child = $trans->item($i); 

                        if ($child->nodeName == $lang) {
                            $this->$name = $child->nodeValue;
                        }
                    }
                }
            }   
        }
    }

    function __get($key){
        if (property_exists('MY_Translation',$this->$key)) {
            return $this->$key;
        } else {
            return $key."_#";
        }
    }

}

XML (just for reference, so it's clear what's happening):

<?xml version="1.0" encoding="UTF-8"?>
<words>
    <word name="thing">
        <en>thing en</en>
        <pt>thing pt</pt>
    </word>
</words>
  • 写回答

1条回答 默认 最新

  • doudou3716 2011-10-27 19:01
    关注

    As you use $this to store the translations, __get is only called on non-existent properties. Change it, the warning you get only notifies you that you're doing something wrong:

    function __get($key)
    {
        return $key."_#";
    }
    

    That's really all you need to do.


    Did you mean

    $this->t->$key
    

    instead of

    $this->$key
    

    ?

    Then you would need to fix the assignment as well:

    $this->t->$name = ...
    

    In general, as you check for the property in the __get function you should not see any warning. The warnings show you that you've made some error in your program logic, so they are useful and you should not disable them not even for a short part of your code. Disabling warnings is not the solution but fixing the code.

    Let me know if this was helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里