21
PHP5.4 And SilverStripe Or me geeking off about the latest thing I managed to compile

SilverStripe PHP5

Embed Size (px)

Citation preview

Page 1: SilverStripe PHP5

PHP5.4 And SilverStripeOr me geeking off about the latest thing I managed

to compile

Page 2: SilverStripe PHP5

Who am I?Simon Welsh - @simon_w

Developer for RentBox

Studying at VUW

http://simon.geek.nz/

Page 3: SilverStripe PHP5
Page 4: SilverStripe PHP5

AgendaTraits

JSON

Other things

Page 5: SilverStripe PHP5

Other thingsBinary number format 0b00101000 0b00000010

Callable type hint function doStuffWithCallback($input, callback $callback) { ... }

Short array syntax [4, 5, ‘index’ => ‘value’]

Improved parse error messages Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in - on line 2

Page 6: SilverStripe PHP5

<?=

Shouldn’t use with SilverStripe, but now always enabled (even without short tags)

Array dereferencing

$secondItem = DataObject::get(‘SiteTree’)[2];

Removed functionality

Safe mode

Magic quotes

Register globals

break/continue $var;

Other things

Page 7: SilverStripe PHP5

JSONConvert::raw2json() just calls json_encode()

Current output isn’t very useful:DataObject: {"destroyed":false,"class":"Member"}DataObjectSet: {"class":"DataObjectSet"}

Could use JSONDataFormatterInstance methods do the convertingNon-recursiveCan’t add information not in a field/relationCode assumes it’s called as part of the API

Page 8: SilverStripe PHP5

JSONThe JsonSerializable interface and jsonSerialize() method tie into json_encode()

A class implementing JsonSerializable returns what gets serialised in the jsonSerialize() method.

Allows for a completely customisable serialisation process.

Page 9: SilverStripe PHP5

JSONAs it is accessed through json_encode(), it allows for recursion.

Convert::raw2json() uses it automatically in 5.4, and it can even be emulated in ≤5.3.

You can define everything that goes into it, wether that’s fields, relations or some other calculated value.

Page 10: SilverStripe PHP5

JSONclass DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider { ......

class DataObjectSet extends ViewableData implements IteratorAggregate, Countable { ......

Page 11: SilverStripe PHP5

JSONclass DataObject extends ViewableData implements DataObjectInterface, i18nEntityProvider, JsonSerializable { public function jsonSerialize() { return $this->record; } ......

class DataObjectSet extends ViewableData implements IteratorAggregate, Countable, JsonSerializable { public function jsonSerialize() { return $this->items; } ......

Page 12: SilverStripe PHP5

JSONBefore:{"destroyed":false,"class":"Member"}{"class":"DataObjectSet"}After:{"ClassName":"Member","Created":"2011-08-20 18:15:59","LastEdited":"2011-08-20 18:35:25","FirstName":"Default Admin","NumVisit":"1","LastVisited":"2011-08-20 22:09:51.437521","Bounced":"0","Locale":"en_NZ","FailedLoginCount":"0","ID":1,"RecordClassName":"Member"}[{"ClassName":"Member","Created":"2011-08-20 18:15:59","LastEdited":"2011-08-20 18:35:25","FirstName":"Default Admin","NumVisit":"1","LastVisited":"2011-08-20 22:09:51.437521","Bounced":"0","Locale":"en_NZ","FailedLoginCount":"0","ID":1,"RecordClassName":"Member"}]

Page 13: SilverStripe PHP5

JSONpublic function jsonSerialize() { $extended = $this->extendedCan('jsonSerialize', null); if($extended !== null) { return $extended; } return $this->record;}[ { "Date": "11 Feb 2011", "Issue": "Grounds", "Notes": "Grounds are very mature and the whole section needs to be ...", "Fixed": true, "FixedDate": "19 Apr 2011", "How": null, "Cost": "$0.00", "Type": "Outdoor Maintenance", "Urgent": false, "Contractor": null, "ID": 9, "Actions": [ { "ID": 7, "Title": "Grounds and stove", "Finished": false, } ] }, { ...

Page 14: SilverStripe PHP5

TraitsHorizontal code reuse

“In computer programming, a trait is a collection of methods, used as a "simple conceptual model for structuring object oriented programs".”

Similar to DataObjectDecorators/Extensions

Page 15: SilverStripe PHP5

TraitsSimilar deceleration to classes and interfaces:trait MyTrait { ... }

Can only define methods, so no properties

Methods can be abstract and static

Method visibility need not be respected

Traits can be composed from other traits

Page 16: SilverStripe PHP5

TraitsTraits inject their methods into the class that is including the trait

Injected methods can be renamed, have their visibility changed and cherry-picked in the case of conflicts

Default precedence for methods is current class, traits, then inherited methods.

Page 17: SilverStripe PHP5

TraitsTraits are mainly for horizontal code reuse. That is having the same code in different classes in different parts of the inheritance structure.

Allows for less code duplication without some nasty inheritance structure.

Page 18: SilverStripe PHP5

Traitsspl_autoload_register(function($className) { if(strtolower(substr($className, -5)) == 'trait') { if(file_exists(__DIR__ . "/traits/$className.php")) { include __DIR__ . "/traits/$className.php"; } }});

Page 19: SilverStripe PHP5

Traitstrait AddressData { public function setAddressData($data) { $data = json_decode($data, true); ... $this->Street = sprintf('%s%s %s', $data['number'], empty($data['alpha']) ? '' : $data['alpha'], $data['street']); ... if($suburb && $suburb->exists()) { $this->SuburbID = $suburb->ID; } else { $suburb = new Suburb; $suburb->Name = $data['suburb']; $suburb->DistrictID = $this->DistrictID; $this->SuburbID = $suburb->write(); } $this->setField('AddressData', json_encode($data)); }}

Page 20: SilverStripe PHP5

Traitsclass Property extends DataObject { use AddressData; public static $db = [ ... ‘AddressData’ => ‘Text’, ...];}

class CustomMember extends DataObjectDecorator { use AddressData; public function extraStatics() { return [‘db’ => [ ... ‘AddressData’ => ‘Text’, ...]]; }}