Created
November 16, 2012 06:15
-
-
Save suin/4084726 to your computer and use it in GitHub Desktop.
PHP5.3のクラスエイリアシングを使った型下位互換技術
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace XCore\Entity; | |
class User | |
{ | |
// 元XoopsUserクラス | |
} | |
class Module | |
{ | |
// 元XoopsModuleクラス | |
} | |
class Group | |
{ | |
// 元XoopsGroupクラス | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Stubs for IDEs | |
/** | |
* @deprecated | |
*/ | |
class XoopsUser extends \XCore\Entity\User {} | |
/** | |
* @deprecated | |
*/ | |
class XoopsModule extends \XCore\Entity\Module {} | |
/** | |
* @deprecated | |
*/ | |
class XoopsGroup extends \XCore\Entity\Group {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Supports for legacy modules, legacy component or legacy library | |
class_alias('\XCore\Entity\User', 'XoopsUser'); | |
class_alias('\XCore\Entity\Module', 'XoopsModule'); | |
class_alias('\XCore\Entity\Group', 'XoopsGroup'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'Entity.php'; | |
require 'LegacySupportPreload.php'; | |
use XCore\Entity\User; | |
use XCore\Entity\Module; | |
use XCore\Entity\Group; | |
// Test: `instanceof` | |
var_dump(new User() instanceof XoopsUser); // true | |
// Test: Type-hinting | |
function accept_user_object(XoopsUser $xoopsUser) { /*...*/ } | |
accept_user_object(new User()); // no exceptions | |
// Test: `is_a()` | |
var_dump(is_a(new User(), 'XoopsUser')); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IDE-sutbs.php
をプロジェクトに置いて、うまくだます。XoopsUser
などに依存しているライブラリやコンポーネントはLegacySupportPreload.php
を置くと動くようになる。LegacySupportPreload.php
を削除すれば、型の下位互換を簡単に破棄できる。