Re: [PHP] Re: How to do type/existence checking in PHP5
* Thus wrote Christian Jul Jensen ([EMAIL PROTECTED]): > > Hi > > Thanks for taking time to look into this. > > [EMAIL PROTECTED] (Red Wingate) writes: > > > > this is a plain design fault on your side: > > I'm sorry, but I don't agree. It is standard in PHP4, and one of the > advantages of having a type loose programming language. Perhaps you misunderstand the 'loose' part of the variables, basically there are 4 types of variables: 1. scalar 2. array 3. object 4. resource How you can access each one differs. > > > Now this makes sense as you first of all would make sure if $a is an > > array and not start with the first index ( which doesn't exist as $a > > is not even an array ) > > > in PHP5, in order not to risk a fatal error, this would have to be: > > $my_array = $some_weird_function_that_returns_a_multidim_array(); > if( is_array($my_array['some']) && > is_array($my_array['some']['special']) && > is_array($my_array['some']['special']['value']) && > is_array($my_array['some']['special']['value']['that']) && > is_array($my_array['some']['special']['value']['that']['i']) && > is_array($my_array['some']['special']['value']['that']['i']['need']) > ) { > apply_logic(); > } You just need: if (is_array($my_array) && is_array($my_array['some']['special']['value']['that']['i']['need']) ) { > > I find this to be a problem, because it makes it hard to migrate > scripts from PHP4, and I'd rather spend my time using the new object > model and XML support, than going through old scripts to make sure > they comply with a new behaviour which I see no good reason for. I think the question is why is this a fatal error versus a simple E_WARNING. Curt -- "I used to think I was indecisive, but now I'm not so sure." -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to do type/existence checking in PHP5
Hi Thanks for taking time to look into this. [EMAIL PROTECTED] (Red Wingate) writes: > this is a plain design fault on your side: I'm sorry, but I don't agree. It is standard in PHP4, and one of the advantages of having a type loose programming language. > Now this makes sense as you first of all would make sure if $a is an > array and not start with the first index ( which doesn't exist as $a > is not even an array ) I understand what the logic behind is, and actually your example, does not give a fatal error as $['foo'] returns a char, and you can call is_array with that. Furthermore it's inconsistent, as it behaves differently depending on which function you call. In PHP4 this is valid $my_array = $some_weird_function_that_returns_a_multidim_array(); if(is_array($my_array['some']['special']['value']['that']['i']['need'])) { apply_logic(); } in PHP5, in order not to risk a fatal error, this would have to be: $my_array = $some_weird_function_that_returns_a_multidim_array(); if( is_array($my_array['some']) && is_array($my_array['some']['special']) && is_array($my_array['some']['special']['value']) && is_array($my_array['some']['special']['value']['that']) && is_array($my_array['some']['special']['value']['that']['i']) && is_array($my_array['some']['special']['value']['that']['i']['need']) ) { apply_logic(); } IMHO that's not very nice. You could argue that ending up in situations like this is bad design, still it's valid in PHP4. You cannot even encapsulate in a function, because that gives a fatal error when you call the function (which makes sense). I find this to be a problem, because it makes it hard to migrate scripts from PHP4, and I'd rather spend my time using the new object model and XML support, than going through old scripts to make sure they comply with a new behaviour which I see no good reason for. -- ./mvh Christian Jul Jensen Frelance webprogrammer TYPO3 Typehead Denmark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to do type/existence checking in PHP5
Hi, this is a plain design fault on your side: allright, just set $a to be a string, thats allright, now you check wether an string-offset ( foo ) is an array or not which causes an FATAL ERROR on PHP5. Now if you wrote some clean code you would have done: Now this makes sense as you first of all would make sure if $a is an array and not start with the first index ( which doesn't exist as $a is not even an array ) -- red PS: PHP5 says: Fatal error: Cannot use string offset as an array in /www/htdocs test_offset.php on line 6 [...] > Hi > > In PHP5 the behaviour of illegal string offsets has changed. This is > documented in the 'thin changes' file. > > This gives a problem in checking for existence / types of values, > directly into a deeper level of a multidimensional array. > > I reported this as a bug[1] because I find the behaviour unfortunate, and > furthermore it's inconsistent. This was refused, with a note 'So don't > do it'. I think it's a really bad idea not to check the > existence/types of values, before using them, so how should this be done > properly in PHP5, without risking fatal errors in the case of a > non-existent array? > > This is a problem in migrating applications from PHP4 because the > error will not appear unless the value deosn't exist, which is exactly > why you do the check. > > [1] http://bugs.php.net/bug.php?id=28107 [...] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: How to do type/existence checking in PHP5
I don't understand the question. One should always check the existence of something before attempting to use it. If (isset($var) && gettype($var) == "boolean") is one way of type checking. "Christian Jul Jensen" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi > > In PHP5 the behaviour of illegal string offsets has changed. This is > documented in the 'thin changes' file. > > This gives a problem in checking for existence / types of values, > directly into a deeper level of a multidimensional array. > > I reported this as a bug[1] because I find the behaviour unfortunate, and > furthermore it's inconsistent. This was refused, with a note 'So don't > do it'. I think it's a really bad idea not to check the > existence/types of values, before using them, so how should this be done > properly in PHP5, without risking fatal errors in the case of a > non-existent array? > > This is a problem in migrating applications from PHP4 because the > error will not appear unless the value deosn't exist, which is exactly > why you do the check. > > [1] http://bugs.php.net/bug.php?id=28107 > > -- > ./mvh Christian Jul Jensen > Frelance webprogrammer > TYPO3 Typehead Denmark -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php