Jump to content

Recommended Posts

How would case-sensitivity (vs. non) be handled?

 

case insensitive (simple match)
$haystack^=$needle (starts with)
$haystack$=$needle (ends with)

case sensitive (exact)
$haystack^==$needle (starts with)
$haystack$==$needle (ends with)

 

No, I don't like that. That is just confusing given the syntax for identical comparison operators. And I think introducing another "variant" is just going to make operators unnecessarily complex. Operators are supposed to be simple.

 

 

Rather than a starts with / ends with operator I'd rather see a regex match operator, such as =~  or something.  Then you could accomplish not only the starts/ends with easily but other patterns too.

 

//starts with
$str =~ /^some string/;

//ends with
$str =~ /some string$/;

 

Just toss the i modifier on for case insensitive.

 

It'd just be nice syntactical sugar though, using the appropriate functions is fine.

 

 

Now that is a great idea.

  • Replies 123
  • Created
  • Last Reply

Top Posters In This Topic

Maybe it is just me but this is starting to turn into readability vs. lazy coders wanting to save a few keystrokes.

Maybe.  But isn't the purpose of programming to be lazy and automated?  I haven't read all of this thread, but a couple ideas here could help all coders become even lazier:D

Well, I'm not arguing we aren't the lazy type. One of my professors used to always say "you'll be a proper engineer whenever you find the easiest way to complete a task." However, when laziness pushes readability past its limits then later on you (or someone else) has to do more work.

 

That said, I am a fan of kicken's code:

//starts with
$str =~ /^some string/;

//ends with
$str =~ /some string$/;

  • 3 weeks later...

Oh yeah I have a few more ideas to add to the wishlist, here they are:

 

1. Method/Function overloading:

I believe this feature has been requested a few times, but for some reason it was never carried out. Overloading is particularly useful for constructors, if you are familiar with C++, C# or Java. I believe PHP can take advantage of it too, so we do not have to write conditional statements to check number and type of arguments over and over again. Using conditionals is fine, but testing the same or similar statement over and over again is a bad OOP practice.

 

2. Public class with executable main() method:

This will enable programmers to enclose everything in a class(or a public class), which in turn encourages programming in the OO way. The application I am working on has some users doing plugins, and I find these plugins quite different from one another in coding convention. Using a few of such plugins together will make the entire script messy. For this reason it is necessary to enforce programming in the OO way, and of course the more advanced way. It is also a huge leap towards fully object oriented programming if every line of code is written in class.

 

3. More magic methods for advanced use:

PHP's magic methods have come in handy for some programmers, and it will be nice to add more magic methods. One idea I have in mind is this __request(), which is quite similar to __call() but differ in a way that it is invoked whenever an existing method is called. It can be quite useful for situations in which you have to declare global variables or load variables from registry class in almost every class instance method. Another one is this __toInt() method, which is similar to __toString() but returns an integer value instead of a string value.

 

4. Array as object by default:

Although PHP has a built-in ArrayObject class, its declaration is quite tedious since you have to create an array and pass it to the constructor of ArrayObject. It would be a better idea that Array as object is the default option for PHP arrays so that we can create an Array Object with the Array() intializer or even the shortened syntax in PHP 5.4. Ideally the array's elements can be accessed using the syntax $array[index] for numeric array, and $array->key for associative array.

 

5. DOM objects support: In PHP packages and frameworks such as Pear and CakePHP, it is possible to create a PHP table, form, link, image and other kinds of DOM objects by writing PHP codes. I've coded my own table class too, and will be doing form next. It would be better for PHP to have built-in classes for DOM objects and optimize them in a way that creating tables and forms are easier and efficient using PHP. Also applicable is built-in AJAX support, it is definitely doable as shown in projects such as xajax and phplivex.

 

What do you think?

Point 5 annoys me. This types of DOM work does not belong in the language itself. This promotes the idea of business being tied to UI.

 

As for built in Ajax support. Ajax requires both server side and client side. Manipulate this idea by trying to implement it server side makes little sense. People who write web application should learn JavaScript.

Point 5 annoys me. This types of DOM work does not belong in the language itself. This promotes the idea of business being tied to UI.

 

As for built in Ajax support. Ajax requires both server side and client side. Manipulate this idea by trying to implement it server side makes little sense. People who write web application should learn JavaScript.

 

I see, you have a very good point. I am suggesting this not because I dont know how to use javascript though. I can code an entire javascript program without PHP just fine. I have trouble using two programming languages together though, and I wonder if this is a rare case or that many people share the same problem.

I have trouble using two programming languages together though, and I wonder if this is a rare case or that many people share the same problem.

 

You don't have to do that with AJAX. AJAX simply sends a request to the server - just as if you browsed to the page yourself. PHP then interprets the request, does whatever, and (optionally) sends a response. Javascript then takes that response (if one was sent) and does whatever with it. At no point during this do the two languages coincide.

 

Now, I don't know what those two libraries you mentioned do, but how can you make this process any simpler on the server side? It's literally the same as a normal page request, there's nothing fancy about it. The only thing you could make easier is the client side, which jQuery and other JS libraries do pretty damn well. Do these two PHP libraries construct the client-side or something?

1. Method/Function overloading:

I believe this feature has been requested a few times, but for some reason it was never carried out. Overloading is particularly useful for constructors, if you are familiar with C++, C# or Java. I believe PHP can take advantage of it too, so we do not have to write conditional statements to check number and type of arguments over and over again. Using conditionals is fine, but testing the same or similar statement over and over again is a bad OOP practice.

 

You can't really have overloading when the language is dynamically typed; C++, C# and Java are all statically typed languages. Might want to suggest static typing first, eh?

 

3. More magic methods for advanced use:

PHP's magic methods have come in handy for some programmers, and it will be nice to add more magic methods.

 

No thank you, there's already enough magic going in PHP if you ask me.

 

It can be quite useful for situations in which you have to declare global variables or load variables from registry class in almost every class instance method.

 

What?? How you can act like you're promoting 'good object orientated programming habits' and then say that?

@ Scoostah: I see, thanks for the reply and I will look into ways to do this then.

 

@ Adam: Sorry about that, it is a mistake. Ideally such 'global' objects or registry objects should pass its reference to whatever classes/objects that use them. In this way every class has a property such as database and page. Another solution is to pass such objects in every method arguments, which is perhaps more tedious.

@ Adam: Sorry about that, it is a mistake. Ideally such 'global' objects or registry objects should pass its reference to whatever classes/objects that use them. In this way every class has a property such as database and page. Another solution is to pass such objects in every method arguments, which is perhaps more tedious.

 

In PHP 5, objects are already passed by reference by default.  It doesn't look like they are, due to the lack of '&' in the syntax, but they are.  All you need for your scenario is dependency injection, which is merely wiring up any dependencies an object has to that object itself.  That's what everyone does when their object has a private $db member and assigns it an actual database connection or object via constructor or other method (as an example)

And moreover, a perfect script requires everything to be an object, thus no procedural programming at all.

 

Nah... a perfect script should stick as close to λ-calculus and functional programming as possible. For instance, clearly this is the most elegant way to print the first 10 Fibonacci numbers:

 

<?php

function Y($f) {
    $ff = function($f) { return $f($f); }; // PHP doesn't allow function call chaining
    return $ff(
        function ($g) use ($f) {
            return $f(function() use ($g) {
                return call_user_func_array($g($g), func_get_args());
            });
        }
    );
}

$fib = Y(function($fib) {
    return function($n) use ($fib) {
        return $n === 0 ? 0 : ($n === 1 ? 1 : $fib($n-1) + $fib($n-2));
    };
});

function tabulate($n, $f) {
    $g = Y(function($a) use ($n, $f) {
        return function($l, $m) use ($n, $a, $f) {
            return $n === $m ? $l : $a(array_merge([$f($m)], $l), $m + 1);
        };
    });
    return $g([], 0);
}

$disp = function($f, $n) {
    printf("f(%d) = %d\n", $n, $f($n));
};

tabulate(10, function($n) use ($fib, $disp) {
    $disp($fib, $n);
});

 

Output:

f(0) = 0
f(1) = 1
f(2) = 1
f(3) = 2
f(4) = 3
f(5) = 5
f(6) = 8
f(7) = 13
f( = 21
f(9) = 34

I think a perfect script should have a better variable naming convention ;)

 

I don't know what this does though:

 

$ff = function($f) { return $f($f); }; // PHP doesn't allow function call chaining
    return $ff(
        function ($g) use ($f) {
            return $f(function() use ($g) {
                return call_user_func_array($g($g), func_get_args());
            });
        }
    );

 

My guess would go to currying of some sort?

I think a perfect script should have a better variable naming convention ;)

 

I don't know what this does though:

 

$ff = function($f) { return $f($f); }; // PHP doesn't allow function call chaining
    return $ff(
        function ($g) use ($f) {
            return $f(function() use ($g) {
                return call_user_func_array($g($g), func_get_args());
            });
        }
    );

 

My guess would go to currying of some sort?

 

It's the Y combinator, so it's called "Y" on purpose. It computes the fixpoint in (untyped) λ-calculus. $f and $g are just functions, so they're properly named as well by standard mathematical conventions.

 

Given to Y is a function that given a "partial" Fibonacci function returns a new function that can compute one more recursive step of the Fibonacci function, so to speak (that is, if it can compute f(n), then the new function can compute f(n+1)). The fixpoint of this function is the "full" Fibonacci function.

 

The Y combinator allows you to express any recursive computation without using recursion.

Thank you for your detailed explanation. I have no experience with lambda-calculus. Bookmarked your code example on the Y combinator. Shouldn't this also be added to that Wikipedia page?

 

$f and $g are just functions, so they're properly named as well by standard mathematical conventions.

 

LOL. I know what kind of face I'd be getting if I said that to my colleagues because I named all my functions y($x) x($y).

Thank you for your detailed explanation. I have no experience with lambda-calculus. Bookmarked your code example on the Y combinator. Shouldn't this also be added to that Wikipedia page?

 

I'm not sure it's worth adding one more example in a new language. Any Turing-complete language will be able to express the Y combinator (though some prettier than others).

 

$f and $g are just functions, so they're properly named as well by standard mathematical conventions.

 

LOL. I know what kind of face I'd be getting if I said that to my colleagues because I named all my functions y($x) x($y).

 

Well, if you know what the Y combinator is, then you'll understand the naming convention of $f and $g. If you don't, giving them other names wouldn't help you at all :P

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.