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);
}