Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/SubClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace Zend\Stdlib;

use ReflectionClass;

/**
* @see https://bugs.php.net/bug.php?id=53727
*
* @category Zend
* @package Zend_Stdlib
* @subpackage SubClass
*/
abstract class SubClass
{

/**
* Checks if the object has this class as one of its parents
*
* @see https://bugs.php.net/bug.php?id=53727
*
* @param string $className
* @param string $type
*/
public static function isSubclassOf($className, $type)
{
if (version_compare(PHP_VERSION, '5.3.7', '>=')) {
return is_subclass_of($className, $type);
}
if (is_subclass_of($className, $type)) {
return true;
}
if (!interface_exists($type)) {
return false;
}
$r = new ReflectionClass($className);
return $r->implementsInterface($type);
}
}
30 changes: 30 additions & 0 deletions test/SubClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Stdlib
*/

namespace ZendTest\Stdlib;

use Zend\Stdlib\SubClass;

class SubClassTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
require_once 'TestAsset/DummySubclasses.php';
}

public function testIsSubclassOf()
{
$test1 = SubClass::isSubclassOf('ZendTest\Stdlib\TestAsset\ChildClass', 'ZendTest\Stdlib\TestAsset\TestInterface');
$test2 = SubClass::isSubclassOf('ZendTest\Stdlib\TestAsset\ParentClass', 'ZendTest\Stdlib\TestAsset\TestInterface');
$this->assertTrue($test1);
$this->assertTrue($test2);
}

}
16 changes: 16 additions & 0 deletions test/TestAsset/DummySubclasses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace ZendTest\Stdlib\TestAsset
{
interface TestInterface
{
}

class ParentClass implements TestInterface
{
}

class ChildClass extends ParentClass
{
}
}

0 comments on commit c7ee17b

Please sign in to comment.