PHP property_exists() 有问题

Posted

技术标签:

【中文标题】PHP property_exists() 有问题【英文标题】:Having a problem with PHP property_exists() 【发布时间】:2010-02-16 23:13:47 【问题描述】:

大家好...当我运行下面的代码时,if(property_exists(get_class($puzzleCharacters_encrypted), $solutionCharacter) 一直评估为 false,但前面的 echo 语句显示正确的信息,所以属性肯定存在。我可能遗漏了什么?(php 版本 5.2.11)

$puzzle_solution = $currentPuzzleData->getVal("text");
$puzzle_encryption = "";
for ($i = 0; $i < strlen($puzzle_solution); $i++)

    $solutionCharacter = substr($puzzle_solution, $i, 1);
    echo ("\$solutionCharacter = " . $solutionCharacter . "<br />\n");
    echo ("\$puzzleCharacters_encrypted->getVal(" . $solutionCharacter . ") = " . $puzzleCharacters_encrypted->getVal($solutionCharacter) . "<br />\n");
    if (property_exists(get_class($puzzleCharacters_encrypted), $solutionCharacter))
    
        $encryptionCharacter = $puzzleCharacters_encrypted->getVal($solutionCharacter);
        $puzzle_encryption .= $encryptionCharacter;
    
    else
    
        $puzzle_encryption .= $solutionCharacter;
    

echo ("<br />\n" . $puzzle_solution);
echo ("<br />\n" . $puzzle_encryption);

谢谢!

【问题讨论】:

也许我在这里遗漏了一些东西,但是property_exists(...,$solutionCharacter)$obj-&gt;getVal($solutionCharacter) 之间的关系是什么? 【参考方案1】:

第二个echo 语句实际上并不是在查询对象,它输出的是一个包含$solutionCharacter 的字符串。这并不能证明该属性确实存在。

然后,您正在查询class$puzzleCharacters_encrypted 中的属性。很可能属性不是在类中定义的,而是在对象中定义的。

如果你尝试会发生什么

if (property_exists($puzzleCharacters_encrypted, $solutionCharacter))

?

【讨论】:

我得到了同样的结果。 $puzzle_solution 和 $puzzle_encryption 打印出相同的值。 另外,我读到在 PHP 我还尝试了以下类参数: $puzzleCharacters(原始类 - 引发错误)“puzzleCharacters” - $puzzle_solution 和 $puzzle_encryption 的结果相同 “puzzleCharacters_encrypted” - $puzzle_solution 和 $谜题加密 @Eric getVal 做什么?您能显示属性实际定义的位置吗? $solutionCharacter 实际上包含什么?您可以发布print_r($puzzleCharacters_encrypted) 的结果吗?那里提到了房产吗?【参考方案2】:

由于代码原因,将此作为答案发布...

class puzzleCharacters

    public $char_a;
    public $char_b;
    public $char_c;
    public $char_d;
    public $char_e;
    public $char_f;
    public $char_g;
    public $char_h;
    public $char_i;
    public $char_j;
    public $char_k;
    public $char_l;
    public $char_m;
    public $char_n;
    public $char_o;
    public $char_p;
    public $char_q;
    public $char_r;
    public $char_s;
    public $char_t;
    public $char_u;
    public $char_v;
    public $char_w;
    public $char_x;
    public $char_y;
    public $char_z;
    public $char_A;
    public $char_B;
    public $char_C;
    public $char_D;
    public $char_E;
    public $char_F;
    public $char_G;
    public $char_H;
    public $char_I;
    public $char_J;
    public $char_K;
    public $char_L;
    public $char_M;
    public $char_N;
    public $char_O;
    public $char_P;
    public $char_Q;
    public $char_R;
    public $char_S;
    public $char_T;
    public $char_U;
    public $char_V;
    public $char_W;
    public $char_X;
    public $char_Y;
    public $char_Z;
    public $char_0;
    public $char_1;
    public $char_2;
    public $char_3;
    public $char_4;
    public $char_5;
    public $char_6;
    public $char_7;
    public $char_8;
    public $char_9;

    public function setVal($prop, $val)
    
        $this->"char_" . $prop = $val;
        //echo ("setting \$this->\$char_" . $prop . " as " . $val . "<br />\n");
    

    public function getVal($prop)
    
        //echo ("getting \$" . $prop . " as " . $this->"char_" . $prop . "<br />\n");
        return ($this->"char_" . $prop);
    

printr 的输出($puzzleCharacters_encrypted):

.puzzleCharacters Object ( [char_a] => x [char_b] => i [char_c] => y [char_d] => j [char_e] => o [char_f] => m [char_g] => p [char_h] => n [char_i] => v [char_j] => l [char_k] => w [char_l] => s [char_m] => u [char_n] => e [char_o] => f [char_p] => h [char_q] => q [char_r] => b [char_s] => k [char_t] => z [char_u] => a [char_v] => r [char_w] => t [char_x] => c [char_y] => d [char_z] => g [char_A] => X [char_B] => I [char_C] => Y [char_D] => J [char_E] => O [char_F] => M [char_G] => P [char_H] => N [char_I] => V [char_J] => L [char_K] => W [char_L] => S [char_M] => U [char_N] => E [char_O] => F [char_P] => H [char_Q] => Q [char_R] => B [char_S] => K [char_T] => Z [char_U] => A [char_V] => R [char_W] => T [char_X] => C [char_Y] => D [char_Z] => G [char_0] => 7 [char_1] => 5 [char_2] => 8 [char_3] => 0 [char_4] => 4 [char_5] => 6 [char_6] => 2 [char_7] => 3 [char_8] => 1 [char_9] => 9 )

【讨论】:

@Eric 那么很明显为什么 propertyExists() 必须失败,因为您在代码中使用char_xyz,但只检查xyz 顺便说一句,将所有这些字符存储在一个数组中会更容易$chars["A"] $chars["B"].... 哦,伙计,我觉得错过了这一点。谢谢!我将 $solutionCharacter 更改为 "char" 。 $solutionCharacter,它就像我想要的那样弹出。我在这个脚本中选择了一个类有两个原因:当使用数组进行字符交换时,我最终使用了六个数组,而使用一个类对象将其减半。另一个原因,您可能已经猜到了,是因为我刚刚开始在 PHP5 中上课。上次我做它们是在 PHP3 中,所以我有点生疏了!

以上是关于PHP property_exists() 有问题的主要内容,如果未能解决你的问题,请参考以下文章

PHP判断一个JSON对象是否含有某一个属性的方法

注意:使用未定义的常量自我假设 'self' ,当放入 property_exists 作为第一个参数时

如何在运行时正确取消设置 PHP 中的类属性?

PHP-property_exists()函数

测试属性是不是存在

获取具有所有属性的 Laravel 模型