PHP property_exists() 出现问题

发布于 2024-08-21 09:20:25 字数 1061 浏览 7 评论 0原文

大家好...当我运行下面的代码时, 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);

谢谢!

Hey all... When I run the code below, the if(property_exists(get_class($puzzleCharacters_encrypted), $solutionCharacter) keeps evaluating to false, but the echo statements preceding that are showing the correct information, so the properties are definitely there. Anything I might be missing? (PHP Version 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);

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界等同你 2024-08-28 09:20:25

第二个 echo 语句实际上并不是查询对象,而是输出一个字符串,其中放置了 $solutionCharacter。这并不能证明该财产确实存在。

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

如果你尝试会发生什么

if (property_exists($puzzleCharacters_encrypted, $solutionCharacter))

The second echo statement is not actually querying the object, it is outputting a string in which $solutionCharacter is put. That is no evidence that the property actually exists.

Then, you are querying for the property in the class of $puzzleCharacters_encrypted. It may well be that the property is not defined in the class, but in the object.

What happens if you try

if (property_exists($puzzleCharacters_encrypted, $solutionCharacter))

?

薄情伤 2024-08-28 09:20:25

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

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 )

Posting this as an answer because of the code...

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});
    }
}

The output of 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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文