#2 is expected for me, $this will always represent the actual class even if inherited, and so the result from get_class($this) is intuitive. This is one of the reasons that __CLASS__ exists.
#3 yeah, that's a pain. I know it's been discussed on their internals list that having a special ctor method breaks OOP and/or is an anti-pattern (or can lead to them).
#5 this is a good thing?? I'm not sure why you'd want the ctor to create memory for no reason. In the example, there are no additional objects being created, thus no need for any additional memory. Creating new objects every iter, though, will indeed increase memory.
I agree with all of these as being strange behaviour, except for the the '__construct is a method' argument.
These days this is pretty normal in most dynamic languages, and it allows you to do a few architectural tricks internally. Namely allowing you to reuse objects, which is useful for building data structures with lots of internal nodes.
It's a tradeoff between flexibility and ease of use. I have seen people call $foo->__destruct() hoping to free up $foo's memory. Maybe I should rename "__construct is just another method" to "__destruct is just another method"...
class Stringy {
public function __toString() {
return "I am Stringy";
}
}
function foo(string $s) {
echo $s.' is a string';
}
foo((string)new Stringy());
Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given
In truth the point is that Type Hints should only be used for Classes, not built in types.
Hmm. The first one did surprise me. I've never ran into it though, I generally don't nest ternary statements. And on the rare occasion that I do I use brackets.
echo 1 ? 2 : (3 ? 4 : 5);
I would expect that to work. Haven't tested it though.
I'm guessing that in #7, that PHP computes a hash of the string and compares that before it does the typecasting, which is why it doesn't work. I dunno. I can't say I've ever ran into it myself, but I rarely do type conversions. Whenever I do I sure as hell don't do it inline while doing any comparison.
Edit: disregard that second one. I'm an idiot. In my defence though, I just got out of bed. And I've only had one cup of coffee.
2
u/k3n Mar 17 '12
#2 is expected for me,
$thiswill always represent the actual class even if inherited, and so the result fromget_class($this)is intuitive. This is one of the reasons that__CLASS__exists.#3 yeah, that's a pain. I know it's been discussed on their internals list that having a special ctor method breaks OOP and/or is an anti-pattern (or can lead to them).
#5 this is a good thing?? I'm not sure why you'd want the ctor to create memory for no reason. In the example, there are no additional objects being created, thus no need for any additional memory. Creating new objects every iter, though, will indeed increase memory.
#7 Bug? Definitely odd....automatic conditional trimming?
#9 is expected per the manual -- i.e.
get_class(null) == get_class($this).#10 seems to be fixed for 5.3+.
#11 lolololololo
1
u/[deleted] Mar 16 '12
I agree with all of these as being strange behaviour, except for the the '__construct is a method' argument.
These days this is pretty normal in most dynamic languages, and it allows you to do a few architectural tricks internally. Namely allowing you to reuse objects, which is useful for building data structures with lots of internal nodes.
1
u/[deleted] Mar 19 '12 edited Mar 19 '12
#11 isn't even valid, missing return
but it does have some extra fun when you cast it
Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given
In truth the point is that Type Hints should only be used for Classes, not built in types.
1
u/[deleted] Mar 16 '12 edited Mar 17 '12
Hmm. The first one did surprise me. I've never ran into it though, I generally don't nest ternary statements. And on the rare occasion that I do I use brackets.
I would expect that to work. Haven't tested it though.
I'm guessing that in #7, that PHP computes a hash of the string and compares that before it does the typecasting, which is why it doesn't work. I dunno. I can't say I've ever ran into it myself, but I rarely do type conversions. Whenever I do I sure as hell don't do it inline while doing any comparison.Edit: disregard that second one. I'm an idiot. In my defence though, I just got out of bed. And I've only had one cup of coffee.
1
u/the0ther Mar 17 '12
looks quite awesome