That depends on the programmer. There's a reason they invented dynamically typed languages. What do you mean by "using $var as string object"?Peec wrote:This makes us PHP writers write ugly code, using $var as string object. PHP is just too dynamic and messy.
PHP suggestion/rant
Moderator: General Moderators
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: PHP suggestion/rant
- greyhoundcode
- Forum Regular
- Posts: 613
- Joined: Mon Feb 11, 2008 4:22 am
Re: PHP suggestion/rant
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.Peec wrote:Oh another thing:
get rid off all _ in functions and put it all in objects and classes.
VSCode: Select all
$str = file_get_contents();Code: Select all
String str = new \php\File('file.txt')->getContent();
-
Blackshawk
- Forum Newbie
- Posts: 5
- Joined: Thu Feb 10, 2011 8:16 am
Re: PHP suggestion/rant
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.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?
Re: PHP suggestion/rant
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.
Which has far from a consistent OO API. Statics, massive numbers of parameters, etc.
Re: PHP suggestion/rant
JQuery should remove the $ !!!
There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
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.
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
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!Zyxist wrote:Arrays passed by reference by default.
I like the way it's done in PHP:
Code: Select all
function byValue($value)Code: Select all
function byAddressOrReference(&$value)
Last edited by VladSun on Thu Feb 10, 2011 2:12 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
We had that in PHP4 - it wasn't a real fun to use.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!
Re: PHP suggestion/rant
Weirdan wrote:We had that in PHP4 - it wasn't a real fun to use.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!
REALLY?!? WHY?
I mean
Code: Select all
function WTF(string $string)
{
$string = "WTF";
}Code: Select all
function WTF2(A_Class $class)
{
$class->string = "WTF";
}
Last edited by VladSun on Thu Feb 10, 2011 2:14 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
clone on write is not really a sensible default for passing objects. I like current behavior better.VladSun wrote:WHY?
Re: PHP suggestion/rant
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!Weirdan wrote:clone on write is not really a sensible default for passing objects. I like current behavior better.VladSun wrote:WHY?
There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
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.
- 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
I won't disagree more with you Zyxist...
Just compare
A scalar value is an object value too... and visa versa

I will repeat it again:
TYPE NEUTRALITY!
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
I will repeat it again:
TYPE NEUTRALITY!
There are 10 types of people in this world, those who understand binary and those who don't
Re: PHP suggestion/rant
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:
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:
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);
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
.
Last edited by VladSun on Fri Feb 11, 2011 5:51 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't