From: "Gary ." on
class Pg_Error
{
private static $errors =
array(INTEGRITY_CONST_UNIQUE => 'uniqueness constraint violated');
....
public static function getMessage($ec)
{
$text = '';
if (array_key_exists($ec, Pg_Error::$errors))
{
$text = Pg_Error::$errors[$ec];
}

return $text;
}
....
}

?

Calling it, the array_key_exists call always returns false:
$this->assertEquals('uniqueness constraint violated',
Pg_Error::getMessage(Pg_Error::INTEGRITY_CONST_UNIQUE));
and I can't see what I've done wrong :(
From: Per Jessen on
Gary . wrote:

> class Pg_Error
> {
> private static $errors =3D
> array(INTEGRITY_CONST_UNIQUE =3D> 'uniqueness constraint
> violated');
> ...
> public static function getMessage($ec)
> {
> $text =3D '';
> if (array_key_exists($ec, Pg_Error::$errors))
> {
> $text =3D Pg_Error::$errors[$ec];
> }
>=20
> return $text;
> }
> ...
> }
>=20
> ?
>=20
> Calling it, the array_key_exists call always returns false:
> $this->assertEquals('uniqueness constraint violated',
> Pg_Error::getMessage(Pg_Error::INTEGRITY_CONST_UNIQUE));
> and I can't see what I've done wrong :(

Might this be better:

public static function getMessage($ec)
{
$text =3D '';
if (array_key_exists($ec, $errors))
{
$text =3D $errors[$ec];
}

return $text;
}



--=20
Per Jessen, Z=C3=BCrich (11.2=C2=B0C)

From: Peter Lind on
On 28 April 2010 08:39, Gary . <php-general(a)garydjones.name> wrote:
> class Pg_Error
> {
>    private static $errors =
>        array(INTEGRITY_CONST_UNIQUE => 'uniqueness constraint violated');
> ...
>    public static function getMessage($ec)
>    {
>        $text = '';
>        if (array_key_exists($ec, Pg_Error::$errors))
>        {
>            $text = Pg_Error::$errors[$ec];
>        }
>
>        return $text;
>    }
> ...
> }
>
> ?
>
> Calling it, the array_key_exists call always returns false:
> $this->assertEquals('uniqueness constraint violated',
> Pg_Error::getMessage(Pg_Error::INTEGRITY_CONST_UNIQUE));
> and I can't see what I've done wrong :(
>

In your code snippet, you do not declare
Pg_Error::INTEGRITY_CONST_UNIQUE - and equally to the point, in the
class you only use INTEGRITY_CONST_UNIQUE in the array, not
Pg_Error::INTEGRITY_CONST_UNIQUE

Regards
Peter

--
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
Flickr: http://www.flickr.com/photos/fake51
BeWelcome: Fake51
Couchsurfing: Fake51
</hype>
From: "Gary ." on
Per Jessen wrote:
> Gary . wrote:

>> Calling it, the array_key_exists call always returns false
....
>> and I can't see what I've done wrong :(
>
> Might this be better:
>
> public static function getMessage($ec)
> {
> $text = '';
> if (array_key_exists($ec, $errors))
> {
> $text = $errors[$ec];
> }
>
> return $text;
> }

Well, not really...

1) Pg_Error_Test::testGetMessage_23505
array_key_exists() expects parameter 2 to be array, null given

(scope issue, I think - it assumes I am talking about a local $errors variable)

> Per Jessen, Zürich (11.2°C)
^^^^^^
Oh :) Maybe I should have just popped round and asked :))
From: "Gary ." on
On 4/28/10, Jochem Maas wrote:
> Op 4/28/10 7:39 AM, Gary . schreef:
>> class Pg_Error
>> {
const INTEGRITY_CONST_UNIQUE = '23505';
>> private static $errors =
>> array(INTEGRITY_CONST_UNIQUE => 'uniqueness constraint violated');
>> ...
>> public static function getMessage($ec)
>> {
>> $text = '';
>> if (array_key_exists($ec, Pg_Error::$errors))
>> {
>> $text = Pg_Error::$errors[$ec];
>> }
>>
>> return $text;
>> }
>> ...
>> }
....
> I'd be looking at making sure the constant 'INTEGRITY_CONST_UNIQUE' is
> actually
> defined before you load the class ...

I accidentally deleted it in the example I posted. Sorry. The actual
code includes the above const. But you put me on the right track, the
array entry wasn't using the const value as a key...