SilverStripe PHP5
Upcoming SlideShare
Loading in...5
×
 

SilverStripe PHP5

on

  • 1,678 views

 

Statistics

Views

Total Views
1,678
Views on SlideShare
1,478
Embed Views
200

Actions

Likes
0
Downloads
5
Comments
0

4 Embeds 200

http://www.silverstripe.org 196
http://silverstripe.org 2
http://www.cms-expert.com 1
http://attheblogzone.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

SilverStripe PHP5 SilverStripe PHP5 Presentation Transcript

  • 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, 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
  • 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;
  • 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
  • JSONThe JsonSerializable interface andjsonSerialize() method tie intojson_encode()A class implementing JsonSerializablereturns what gets serialised in thejsonSerialize() method.Allows for a completely customisableserialisation process.
  • 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.
  • JSONclass DataObject extends ViewableData implementsDataObjectInterface, i18nEntityProvider { ......class DataObjectSet extends ViewableData implementsIteratorAggregate, Countable { ......
  • 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; } ......
  • 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"}]
  • 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, } ] }, { ...
  • 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
  • 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
  • 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.
  • 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.
  • Traitsspl_autoload_register(function($className) { if(strtolower(substr($className, -5)) == trait) { if(file_exists(__DIR__ . "/traits/$className.php")) { include __DIR__ . "/traits/$className.php"; } }});
  • 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)); }}
  • Traitsclass Property extends DataObject { use AddressData; public static $db = [ ... ‘AddressData’ => ‘Text’, ...];}class CustomMember extends DataObjectDecorator { use AddressData; public function extraStatics() { return [‘db’ => [ ... ‘AddressData’ => ‘Text’, ...]]; }}
  • 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