PHP5.4 And SilverStripeOr me geeking off about the latest thing I managed                   to compile
Who am I?Simon Welsh - @simon_wDeveloper for RentBoxStudying at VUWhttp://simon.geek.nz/
AgendaTraitsJSONOther things
Other thingsBinary number format      0b00101000 0b00000010Callable type hint      function doStuffWithCallback($input, ca...
Other things<?=  Shouldn’t use with SilverStripe, but now always  enabled (even without short tags)Array dereferencing  $s...
JSONConvert::raw2json() just calls json_encode()Current output isn’t very useful:DataObject: {"destroyed":false,"class":"M...
JSONThe JsonSerializable interface andjsonSerialize() method tie intojson_encode()A class implementing JsonSerializableret...
JSONAs it is accessed through json_encode(),it allows for recursion.Convert::raw2json() uses it automaticallyin 5.4, and i...
JSONclass DataObject extends ViewableData implementsDataObjectInterface, i18nEntityProvider {   ......class DataObjectSet ...
JSONclass DataObject extends ViewableData implementsDataObjectInterface, i18nEntityProvider,JsonSerializable {   public fu...
JSONBefore:{"destroyed":false,"class":"Member"}{"class":"DataObjectSet"}After:{"ClassName":"Member","Created":"2011-08-201...
JSONpublic function jsonSerialize() {	 	      $extended = $this->extendedCan(jsonSerialize, null);	 	      if($extended !=...
TraitsHorizontal code reuse“In computer programming, a trait is acollection of methods, used as a "simpleconceptual model ...
TraitsSimilar deceleration to classes andinterfaces:trait MyTrait { ... }Can only define methods, so no propertiesMethods c...
TraitsTraits inject their methods into the classthat is including the traitInjected methods can be renamed, havetheir visi...
TraitsTraits are mainly for horizontal codereuse. That is having the same code indifferent classes in different parts ofth...
Traitsspl_autoload_register(function($className) {	     if(strtolower(substr($className, -5)) == trait) {	     	   if(file...
Traitstrait AddressData {	   public function setAddressData($data) {	   	   $data = json_decode($data, true);	   	   ...	 ...
Traitsclass Property extends DataObject {	   use AddressData;	   public static $db = [ ...	   	   ‘AddressData’ => ‘Text’,...
More InformationPHP documentation:    http://php.net/JsonSerializable    http://php.net/traitsCurrent change log:    http:...
SilverStripe PHP5
Upcoming SlideShare
Loading in …5
×

SilverStripe PHP5

1,576
-1

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,576
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SilverStripe PHP5

  1. 1. PHP5.4 And SilverStripeOr me geeking off about the latest thing I managed to compile
  2. 2. Who am I?Simon Welsh - @simon_wDeveloper for RentBoxStudying at VUWhttp://simon.geek.nz/
  3. 3. AgendaTraitsJSONOther things
  4. 4. Other thingsBinary number format 0b00101000 0b00000010Callable 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
  5. 5. Other things<?= 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;
  6. 6. JSONConvert::raw2json() just calls json_encode()Current output isn’t very useful:DataObject: {"destroyed":false,"class":"Member"}DataObjectSet: {"class":"DataObjectSet"}Could use JSONDataFormatter Instance methods do the converting Non-recursive Can’t add information not in a field/relation Code assumes it’s called as part of the API
  7. 7. JSONThe JsonSerializable interface andjsonSerialize() method tie intojson_encode()A class implementing JsonSerializablereturns what gets serialised in thejsonSerialize() method.Allows for a completely customisableserialisation process.
  8. 8. JSONAs it is accessed through json_encode(),it allows for recursion.Convert::raw2json() uses it automaticallyin 5.4, and it can even be emulated in ≤5.3.You can define everything that goes intoit, wether that’s fields, relations or someother calculated value.
  9. 9. JSONclass DataObject extends ViewableData implementsDataObjectInterface, i18nEntityProvider { ......class DataObjectSet extends ViewableData implementsIteratorAggregate, Countable { ......
  10. 10. JSONclass DataObject extends ViewableData implementsDataObjectInterface, i18nEntityProvider,JsonSerializable { public function jsonSerialize() { return $this->record; } ......class DataObjectSet extends ViewableData implementsIteratorAggregate, Countable, JsonSerializable { public function jsonSerialize() { return $this->items; } ......
  11. 11. JSONBefore:{"destroyed":false,"class":"Member"}{"class":"DataObjectSet"}After:{"ClassName":"Member","Created":"2011-08-2018:15:59","LastEdited":"2011-08-2018:35:25","FirstName":"DefaultAdmin","NumVisit":"1","LastVisited":"2011-08-2022:09:51.437521","Bounced":"0","Locale":"en_NZ","FailedLoginCount":"0","ID":1,"RecordClassName":"Member"}[{"ClassName":"Member","Created":"2011-08-2018:15:59","LastEdited":"2011-08-2018:35:25","FirstName":"DefaultAdmin","NumVisit":"1","LastVisited":"2011-08-2022:09:51.437521","Bounced":"0","Locale":"en_NZ","FailedLoginCount":"0","ID":1,"RecordClassName":"Member"}]
  12. 12. 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, } ] }, { ...
  13. 13. TraitsHorizontal code reuse“In computer programming, a trait is acollection of methods, used as a "simpleconceptual model for structuring objectoriented programs".”Similar to DataObjectDecorators/Extensions
  14. 14. TraitsSimilar deceleration to classes andinterfaces:trait MyTrait { ... }Can only define methods, so no propertiesMethods can be abstract and staticMethod visibility need not be respectedTraits can be composed from other traits
  15. 15. TraitsTraits inject their methods into the classthat is including the traitInjected methods can be renamed, havetheir visibility changed and cherry-pickedin the case of conflictsDefault precedence for methods is currentclass, traits, then inherited methods.
  16. 16. TraitsTraits are mainly for horizontal codereuse. That is having the same code indifferent classes in different parts ofthe inheritance structure.Allows for less code duplication withoutsome nasty inheritance structure.
  17. 17. Traitsspl_autoload_register(function($className) { if(strtolower(substr($className, -5)) == trait) { if(file_exists(__DIR__ . "/traits/$className.php")) { include __DIR__ . "/traits/$className.php"; } }});
  18. 18. 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)); }}
  19. 19. Traitsclass Property extends DataObject { use AddressData; public static $db = [ ... ‘AddressData’ => ‘Text’, ...];}class CustomMember extends DataObjectDecorator { use AddressData; public function extraStatics() { return [‘db’ => [ ... ‘AddressData’ => ‘Text’, ...]]; }}
  20. 20. More InformationPHP documentation: http://php.net/JsonSerializable http://php.net/traitsCurrent change log: http://svn.php.net/viewvc/php/php-src/branches/ PHP_5_4/NEWS?view=markupSVN Instructions: http://php.net/svn.phpThese slides: http://simon.geek.nz/silverstripe-and-php5-4-talk/ http://assets.simon.geek.nz/Talks/SilverStripe-PHP5.4.pdf

×