Skip to content

Instantly share code, notes, and snippets.

@auroraeosrose
Created July 1, 2015 19:07
Show Gist options
  • Save auroraeosrose/2036d1d675a4bd254450 to your computer and use it in GitHub Desktop.
Save auroraeosrose/2036d1d675a4bd254450 to your computer and use it in GitHub Desktop.
Struct
<?php
// Basically a strict object with no methods
class Struct {
public $foo;
public $bar;
}
$struct = new Struct(['foo' => 'something']); // constructor args would get pushed to properties ???
$struct->foo = 'whatever';
$struct->no = 'foo'; // This would throw an exception
// But how woudl you type it? perhaps a special method to get teh definition?
class Struct {
public $foo;
public $bar;
protected function define() {
return ['foo' => 'int',
'bar' => 'mixed'];
}
$struct = new Struct();
$struct->bar = 'hi!'; // ok
$struct->foo = 2; // ok
$struct->foo = 'boo'; // exception
@Crell
Copy link

Crell commented Jul 10, 2015

Well, the concern with property access on classes is that determining at runtime what was a property and what was a bare object member was prohibitively costly. My thinking is that if structs are a new thing, baking property accessors in from the get-go sidesteps that question. (And it's on value objects where they would be most useful anyway.) That said, they can/should be optional. If all you want is the default behavior of write-to-property/read-from-property, sure, leave them out.

Traits vs. multiple inheritance: Meh, as long as I can mix multiple pieces together to make one big piece I'm impartial on the details. I'm thinking, eg, cases where I've had Doctrine objects with 4-5 traits in them for fields that are shared between many entities (one property, 2 methods, plus annotations), but inheritance is a wrong/unworkable tool. Traits are fantastic for that, I found. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment