Req #55266 [Com]: Traits vs Type hints

2011-07-22 Thread alex dot howansky at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=55266edit=1

 ID: 55266
 Comment by: alex dot howansky at gmail dot com
 Reported by:mchlpl at gmail dot com
 Summary:Traits vs Type hints
 Status: Open
 Type:   Feature/Change Request
 Package:Class/Object related
 PHP Version:5.4.0alpha2
 Block user comment: N
 Private report: N

 New Comment:

 $a is not an instance of the trait but rather of
 the class that utilizes the trait.

You can say the same of interfaces and abstracts, but is_a returns true for 
them.

Test script:
---
trait someTrait {}

interface someInterface {}

abstract class someAbstract {}

class someClass extends someAbstract implements someInterface {
use someTrait;
}

$a = new someClass();
var_dump(is_a($a, 'someClass'));
var_dump(is_a($a, 'someAbstract'));
var_dump(is_a($a, 'someInterface'));
var_dump(is_a($a, 'someTrait'));

Expected result:

bool(true)
bool(true)
bool(true)
bool(?)

Actual result:

bool(true)
bool(true)
bool(true)
bool(false)


Previous Comments:

[2011-07-22 14:50:36] mchlpl at gmail dot com

Mike: That's one way of looking at it. My point of view is that a trait adds 
methods to class' interface (where interface is a set of public members of a 
class - both methods and fields) and this should be reflected in class type.

Currently (unless I missed something that was not mentioned in RFC - I admit to 
not have gone through SVN version of docs) there seem to be no way to check if 
class uses a trait or no. Only new functions mentioned are trait_exists() and 
get_declared_traits(). Checking if the object has a method will not work, if 
trait is meant to override method in host class. Having traits reflected in 
object's type, would solve this problem nicely. Adding another function to 
glabal namespace (uses_trait() ?) is not something I would like to see.


[2011-07-22 13:36:59] me at mikestowe dot com

I believe this is the correct result as $a is not an instance of the trait but 
rather of the class that utilizes the trait.


[2011-07-22 07:44:34] mchlpl at gmail dot com

Description:

Traits, when used in a class, are not added to class' type, and therefore can 
not be use in type hints. The RFCs on traits https://wiki.php.net/rfc/traits  
and on horizontal reuse https://wiki.php.net/rfc/horizontalreuse do not explain 
if this is by design.

On the other hand Traits seem to share the same namespace as Classes and 
Interfaces, since it is impossible to have an Interface and a Trait that share 
the name ('Fatal error: Cannot redeclare class' is raised when this is 
attempted).

Test script:
---
?php
trait SomeTrait {}

class SomeClass {
  use SomeTrait;
}

$a = new SomeClass();

var_dump(is_a($a,'SomeTrait'));

Expected result:

bool(true)

Actual result:
--
bool(false)






-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55266edit=1


Bug #49143 [Com]: is_callable() and unnecessary backslash

2010-06-10 Thread alex dot howansky at gmail dot com
Edit report at http://bugs.php.net/bug.php?id=49143edit=1

 ID:   49143
 Comment by:   alex dot howansky at gmail dot com
 Reported by:  david at grudl dot com
 Summary:  is_callable() and unnecessary backslash
 Status:   Open
 Type: Bug
 Package:  Class/Object related
 Operating System: *
 PHP Version:  5.3.0

 New Comment:

This bug is still present in 5.3.2 and also affects instantiating a new
class 

from a string variable:



function __autoload($class)

{

echo $class\n;

}



$obj = new MyProject\Thing();

// correct: MyProject\Thing



$obj = new \MyProject\Thing();

// correct: MyProject\Thing



$class = '\\MyProject\\Thing';

$obj = new $class();

// wrong: \MyProject\Thing


Previous Comments:

[2010-04-09 08:18:22] abca_b_cabcom at hotmail dot com

hi david, you are not in a namespace it is just a classname.


[2009-08-04 11:24:35] david at grudl dot com

The two same class invokes autoloader with different class name (maybe 

better term is non-canonicalized class name).



This means: every autoloader must call $name = ltrim($name, \).



defined() removes leading \ automatically, is_callable and 

method_exists do not.


[2009-08-04 11:16:07] j...@php.net

Yes, which problem..? I don't see any errors. 


[2009-08-04 00:34:46] david at grudl dot com

Tested with 5.3.1-dev (Sun, 02 Aug 2009 18:53:57 +), problem still 

exists.


[2009-08-03 16:19:59] david at grudl dot com

Description:

is_callable() and method_exists() may invoke autoloader with 
unnecessary namespace backslash.



\My\MyClass::func() is the same as My\MyClass::func().









Reproduce code:
---
function __autoload($name)

{

echo $name;

}



is_callable('\My\MyClass::func'); // ERROR



is_callable('My\MyClass::func'); // OK



method_exists('\My\MyClass', 'func'); // ERROR



method_exists('My\MyClass', 'func'); // OK



// defined works well:

defined('\My\MyClass::CONST'); // OK



defined('My\MyClass::CONST'); // OK





Expected result:

My\MyClass

My\MyClass



My\MyClass

My\MyClass



My\MyClass

My\MyClass

Actual result:
--
\My\MyClass

My\MyClass



\My\MyClass

My\MyClass



My\MyClass

My\MyClass






-- 
Edit this bug report at http://bugs.php.net/bug.php?id=49143edit=1