Page 2 of 4

Re: PHP suggestion/rant

Posted: Sun Jan 30, 2011 11:02 am
by Jonah Bron
Peec wrote:This makes us PHP writers write ugly code, using $var as string object. PHP is just too dynamic and messy.
That depends on the programmer. There's a reason they invented dynamically typed languages. What do you mean by "using $var as string object"?

Re: PHP suggestion/rant

Posted: Wed Feb 02, 2011 1:39 pm
by greyhoundcode
Peec wrote:Oh another thing:
get rid off all _ in functions and put it all in objects and classes.

Code: Select all

$str = file_get_contents();
VS

Code: Select all

String str = new \php\File('file.txt')->getContent();
I suppose everyone has there own opinion on this sort of thing. Speaking for myself file_get_contents('important.xml.data') is clear enough as it is. A highly consistent OO API isn't everything, and the popularity of PHP surely testifies that it is not too onerous as is.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 10:26 am
by Blackshawk
Being able to mixin methods from the global namespace or something. What happens then when two libraries need to define a method called .foo() ? Does the latter defined method replace the one that was defined first?
Not that I'm advocating any such situation but namespaced traits could solve this problem, as we're probably going to get traits in the next major release of PHP.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 12:21 pm
by Jenk
I find it quite ironic that Peec is complaining of terrible API, when his own framework has APIs such as on this page: http://code.google.com/p/peecfw/wiki/Forms

Which has far from a consistent OO API. Statics, massive numbers of parameters, etc.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 1:40 pm
by VladSun
:twisted:
JQuery should remove the $ !!!

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 1:56 pm
by Zyxist
Lack of "$" and static types? There is such a language. It's called Java. I don't understand... if I need and want a hammer, I choose hammer instead of, for example, choosing a screwdriver and complaining that it is very hard to drive nails with it. Think about it, people.

Of course, if I had to design a PHP from scratch, I would change many things, but there only a couple of really critical issues:

- Replacing those #$% warnings with real exceptions.
- Promising me that the new extensions will be designed by people who not only know, what OOP is, but also - how to use it. The example of SPL shows that many PHP developers have little trouble with this concept.
- Getting rid of magic quotes and safe mode.
- Arrays passed by reference by default.
- try ... finally block.
- Better support for Unicode.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:09 pm
by VladSun
Zyxist wrote:Arrays passed by reference by default.
We need to have an unified rule about passing arguments. We need to have the power of passing any parameter (array, object scalar) in the way we want - by address/reference by value. And it should be clear by the function/method signature!

I like the way it's done in PHP:

Code: Select all

function byValue($value)
vs

Code: Select all

function byAddressOrReference(&$value)
But it should be done in a single, unified way.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:12 pm
by Weirdan
VladSun wrote:We need to have an unified rule about passing arguments. I need to have the power of passing any parameter (array, object scalar) in the way I want - by address/reference by value. And it should be clear by the function/method signature!
We had that in PHP4 - it wasn't a real fun to use.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:12 pm
by VladSun
Weirdan wrote:
VladSun wrote:We need to have an unified rule about passing arguments. I need to have the power of passing any parameter (array, object scalar) in the way I want - by address/reference by value. And it should be clear by the function/method signature!
We had that in PHP4 - it wasn't a real fun to use.

REALLY?!? WHY?
I mean

Code: Select all

function WTF(string $string)
{
    $string = "WTF";
}

Code: Select all

function WTF2(A_Class $class)
{
    $class->string = "WTF";
}

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:14 pm
by Weirdan
VladSun wrote:WHY?
clone on write is not really a sensible default for passing objects. I like current behavior better.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:19 pm
by VladSun
Weirdan wrote:
VladSun wrote:WHY?
clone on write is not really a sensible default for passing objects. I like current behavior better.
I did not argue what should be the default method. I've just argued about the "type neutrality"... I.e. I really don't care what should be the "default" passing by method in function/method arguments. I just want it to be clear - by reference or by value, no matter what the TYPE of the argument is!

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:46 pm
by Zyxist
I disagree with VladSun about passing the arguments. PHP4 showed that this is a road to nowhere. OOP in PHP4 was almost unusable due to passing objects by value as the default behaviour. Note that every existing PHP type falls into one of those two categories:

- Scalars
- Objects

Scalars are real values, sequences of bytes that have a well-defined meaning. The scalar type defines the actual value representation, and the conversion between different scalar types is possible in all the directions. Sometimes, of course we loose some part of the information, but the point is that this is still a single, meaningful value.

On the other hand, we have objects which are compound types. Objects are not values by definition, but rather some containers. Notice that although we can convert an object to string (sometimes), we cannot perform a conversion in the opposite direction, so they can have a value, but they are not that value. For objects, it is much more natural to pass them by reference, just like in the real life. Personally, I do not clone a pen, if I want to give it to someone :).

There is one interesting thing to say. From technical point of view, there is absolutely no reason to keep arrays as a separate type. It is only a historical issue. Arrays behave like normal objects; they are simply one more data structure, just like the others from the SPL extension. The same can be said about resources.

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 2:49 pm
by VladSun
I won't disagree more with you Zyxist...

Just compare

Code: Select all

function a($b)
{ $b  = 3};

Code: Select all

function a(&$b)
{ $b  = 3};

A scalar value is an object value too... and visa versa :P :)

I will repeat it again:

TYPE NEUTRALITY!

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 3:57 pm
by Zyxist
OK, let's assume that object is a value. I'm going to prove you that what you say is against logic and nature :) and you have to pass scalars by values and non-scalars by references.

Assumptions:
- Scalar is an object, too => everything is an object.
- Type neutrality: only one way to pass the arguments.

Hypothesis:
- Non-scalars are passed by references.
- Scalars are passed by values.

Proof:

1. If everything is an object, then every possible value, that is, a sequence of bytes, can be represented by different objects. For example, we can say that a physical sequence of data "11011011" is called "219" of class "integer".
2. We can't talk about "passing by values", because values are some virtual concepts derived in our model. We cannot use concepts from the model to describe the model structure itself. So the only way of passing the arguments is passing by reference.
3. OK, we have proved the first part. Now we have a consistent, type-neutral argument passing. Objects are passed by references which means that every variable is also a reference to some object stored "somewhere", just like the objects in PHP.
4. But let's consider the example:

Code: Select all

function x($foo)
{
    $foo = $foo + 50;
}
$bar = 40;
foo($bar);
5. $foo + 50 does not modify the "40" object, but returns another object, "90", and stores a reference to it under $foo.
6. Notice that $foo and $bar are different variables. In order to see the change from the outside, we would have to modify the "40" object, but we did not modify it, but just put a reference to some other object into "$bar". So, $bar still points to object "40".
7. Surprise. It turns out that passing "scalars" by references has the same properties, as "passing by value". This proves the second part of the hypothesis. Passing scalars by value turns out to be a natural property of this concept.

Note that the model, where we allow passing by value only, is inconsistent. We have already showed that objects can be used to model values, and if we actually use objects to model values, we can't say that these objects are passed by values, because we do not have a concept of "value" yet. This is a fact from logic: we cannot make statements about some language in this language itself (famous paradox "This sentence is false"), we cannot use the hypothesis to prove itself, etc.

Yeah, while real mathematics is definitely not for normal people, it is actually very useful :).
----
By the way, references to scalars can be also modelled as objects. The second code you have provided can be also implemented as:

Code: Select all

function a(Reference $b)
{
  $b->set(3);
}

Re: PHP suggestion/rant

Posted: Thu Feb 10, 2011 4:16 pm
by VladSun
.