rfc:traits

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:traits [2009/01/18 17:03] – Updated syntax according to the patch gronrfc:traits [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Request for Comments: Traits for PHP ====== +====== Request for Comments: Traits for PHP (Superseded by Horizontal Reuse for PHP) ======
-This RFC is outdated and replaced by [[rfc:HorizontalReuse|RFC: Horizontal Reuse for PHP]].+
  
   * Version: 1.5   * Version: 1.5
   * Date: 2008-03-06   * Date: 2008-03-06
   * Author: Stefan Marr <php.at.stefan-marr.de>   * Author: Stefan Marr <php.at.stefan-marr.de>
-  * Status: Patch available, Superseded by [[rfc:HorizontalReuse|Horizontal Reuse for PHP]]+  * Status: **Superseded by [[rfc:HorizontalReuse|Horizontal Reuse for PHP]]**
   * Patch: http://www.stefan-marr.de/archives/20-New-Traits-Patch-Ready-for-Testing.html   * Patch: http://www.stefan-marr.de/archives/20-New-Traits-Patch-Ready-for-Testing.html
   * First Published at: http://www.stefan-marr.de/artikel/rfc-traits-for-php.html   * First Published at: http://www.stefan-marr.de/artikel/rfc-traits-for-php.html
Line 16: Line 15:
 additional resources about Traits are given. additional resources about Traits are given.
  
 +**This RFC is outdated and replaced by [[rfc:HorizontalReuse|RFC: Horizontal Reuse for PHP]].**
 ===== Introduction ===== ===== Introduction =====
  
Line 504: Line 504:
  
 This section collects proposals for alternative Traits syntaxes. This section collects proposals for alternative Traits syntaxes.
 +
 +==== Scala Synthax and practical example ====
 +
 +This is inpired from [[http://www.scala-lang.org/node/117]].
 +
 +<code php>
 +trait User_Comments 
 +{
 +     function addComment($c)
 +     {
 +           $db = App::getDb();   
 +           $db->Execute("INSERT INTO user_comments (type, type_id, user_id, text)VALUES ($c->type, $c->type_id, $c->uid, $c->text)");
 +     }
 +
 +     function getComments($filter)
 +     {
 +           return array();
 +     }
 +
 +     function removeComment($id)
 +     {
 +           $db = App::getDb();   
 +           $db->Execute("DELETE FROM user_comments ... ");
 +     }
 +}
 +
 +class App_Email with User_Comments {}
 +
 +class App_Document with User_Comments  {
 +
 +     function removeComment($id)
 +     {
 +           $db = App::getDb();   
 +           $db->Execute("DELETE FROM user_comments ... ");
 +
 +           $db->Execute("UPDATE app_documents SET comments_count = ... ");
 +     }
 +}
 +// PHP notice : 'User_Comments::removeComment() definition skipped in App_Document'
 +
 +$doc = new App_Document;
 +
 +echo is_a($doc, 'App_Document');     // true
 +echo is_a($doc, 'User_Comments');    // false
 +echo is_with($doc, 'User_Comments'); // true
 +</code>
 +
 +We deal with conflicting class definitions by simply ignoring them.
 +
 +<code php>
 +trait Conflict_Comments 
 +{
 +     function removeComment($id)
 +     {
 +           return false;
 +     }
 +}
 +
 +class App_Document_Conflict with User_Comments, Conflict_Comments  {
 +
 +     function removeComment($id)
 +     {
 +           $db = App::getDb();   
 +           $db->Execute("DELETE FROM user_comments ... ");
 +
 +           $db->Execute("UPDATE app_documents SET comments_count = ... ");
 +     }
 +}
 +// PHP notice : 'User_Comments::removeComment() definition skipped in App_Document_Conflict'
 +// PHP notice : 'Conflict_Comments::removeComment() definition skipped in App_Document_Conflict'
 +
 +$doc = new App_Document_Conflict;
 +
 +echo is_a($doc, 'App_Document_Conflict');// true
 +echo is_with($doc, 'User_Comments');     // true
 +echo is_with($doc, 'Conflict_Comments'); // true
 +</code>
  
 === Alternative Keywords for use === === Alternative Keywords for use ===
rfc/traits.1232298209.txt.gz · Last modified: 2017/09/22 13:28 (external edit)