7 Replies - 5490 Views - Last Post: 17 March 2012 - 04:58 AM

#1 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

What would you like to see in PHP?

Posted 16 March 2012 - 07:23 PM

I recently had a small problem that got me thinking about ways to achieve a task. I wanted to loop through a series of validators and check that each one is true. Which got me thinking "wouldn't it be great if there was a boolean concatentor in PHP?". So, figuring that other people might have wishlists for PHP, I figured I'd create this thread.

My idea is that there are two new operators: &= and |=. They'd work like this:
$booleanArray = array(true, true, false);
$andCondition = true;
$orCondition = false;
foreach($booleanArray as $bool)
{
  $andCondition &= $bool;
  $orCondition |= $bool;
}
// $andCondition is now false, since T & (T & T & F) = F
// $orCondition is now true, since F | (T | T | F) = T


(Fingers crossed Dorm doesn't weigh in with a "it already exists" comment).

What would you like to see in PHP?

This post has been edited by Dormilich: 17 March 2012 - 03:18 AM
Reason for edit:: Rep++ for the comment


Is This A Good Question/Topic? 1
  • +

Replies To: What would you like to see in PHP?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 03:04 AM

View Poste_i_pi, on 17 March 2012 - 03:23 AM, said:

(Fingers crossed Dorm doesn't weigh in with a "it already exists" comment).

lol (5th line from below)

mind that | and & are bitwise operators.


ok, for the fun of it: "it already exists"

This post has been edited by Dormilich: 17 March 2012 - 03:17 AM

Was This Post Helpful? 2
  • +
  • -

#3 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 03:26 AM

Hehe good one Dormilich. I knew those operators existed but I didn't consider that they'd apply here. (I'm never sure about that bitwise stuff...)

I was playing around with e_i_pi's code earlier, trying to find the simplest way to achieve what he was talking about with what we currently have, but it never occurred to me to actually try the original example :D

Just for the record, my favorite version was this:
$values = array(true, true, false);

$and_result = array_reduce($values, function($result, $item) {
	return $result && $item;
}, true);

$or_result = array_reduce($values, function($result, $item) {
	return $result || $item;
}, false);

var_dump((boolean)$and_result); // false
var_dump((boolean)$or_result);  // true


Has a sort of Javascript feel to it :P

e_i_pi said:

What would you like to see in PHP?

The ability to bind primitive variable types to function parameters and function return values. I know this is a strange thing to want for a loosely-typed language, but still...

O, and of course, a sudden and permanent death to the old MySQL extension.

(Aim high, right...)
Was This Post Helpful? 1
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 03:31 AM

View PostAtli, on 17 March 2012 - 11:26 AM, said:

e_i_pi said:

What would you like to see in PHP?

The ability to bind primitive variable types to function parameters and function return values. I know this is a strange thing to want for a loosely-typed language, but still...

you can do that manually by using &$parameter in the declaration (for parameters)


View PostAtli, on 17 March 2012 - 11:26 AM, said:

O, and of course, a sudden and permanent death to the old MySQL extension.

the more sudden and the more permanent, the better (best coupled with an electrical feedback)
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 03:51 AM

View PostDormilich, on 17 March 2012 - 10:31 AM, said:

View PostAtli, on 17 March 2012 - 11:26 AM, said:

e_i_pi said:

What would you like to see in PHP?

The ability to bind primitive variable types to function parameters and function return values. I know this is a strange thing to want for a loosely-typed language, but still...

you can do that manually by using &$parameter in the declaration (for parameters)

I may have phrased that rather poorly. What I meant was type-hinting with primitive values, like we can do now with objects. So we could do stuff like:
class foo {
    public function subtract(int $x, int $y) {
        return $x - $y;
    }

    /* Or even, more extremely: */
    public int subtract(int $x, int $y) {
        return $x - $y;
    }
}


I know that second function is rather unlikely, but for the first function, it's been bugging me ever since type-hinting was originally introduced why they didn't include primitive values in that deal.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 03:58 AM

I donít know how PHP actually does the type checking, but it indeed conflicts with the concept of a loosely-typed language. besides the fact that when you get a number through $_GET/Ö, it is a string and not an int/float/double/Ö. while objects canít easily switch types, strings and numbers can (PHP should not throw an error for int "1", maybe thatís why).

there is a somewhat awkward workaround using the experimental SPL_Types extension.
Was This Post Helpful? 0
  • +
  • -

#7 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3716
  • View blog
  • Posts: 5,976
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 04:37 AM

I was thinking more along the lines of PHP doing it's uber implicit conversion deal on the input parameters, rather than it trowing errors when passing incorrect types. So that if I specify that I want an integer into my function, if the user passes $_GET["id"] with it's string value, PHP would just take care of the conversion silently.

All I'm interested in is forcing the input value to be a specific type. I hate having to do:
function doStuffWithID($id) {
    if (!is_int($id)) {
        trigger_error("Use an integer, fool!", E_USER_OMGWTFBBQ!);
    }
}


I just want PHP to guarantee that $id is an actual integer.

Quote

there is a somewhat awkward workaround using the experimental SPL_Types extension.

That looks interesting. I'll have to play around with that a bit and see if that's a viable alternative. Thanks for pointing that out! :)
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3515
  • View blog
  • Posts: 10,146
  • Joined: 08-June 10

Re: What would you like to see in PHP?

Posted 17 March 2012 - 04:58 AM

View PostAtli, on 17 March 2012 - 12:37 PM, said:

I was thinking more along the lines of PHP doing it's uber implicit conversion deal on the input parameters, rather than it trowing errors when passing incorrect types. So that if I specify that I want an integer into my function, if the user passes $_GET["id"] with it's string value, PHP would just take care of the conversion silently.

if I want that, Iíll use validating filter functions. if I specify INT [0, 100] I get that or false/null.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1