Req #62254 [Opn->Wfx]: Allow explicit call of a trait methods

From: Date: Mon, 16 Aug 2021 12:19:57 +0000
Subject: Req #62254 [Opn->Wfx]: Allow explicit call of a trait methods
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-235844@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62254&edit=1

 ID:                 62254
 Updated by:         cmb@php.net
 Reported by:        jachym dot tousek at gmail dot com
 Summary:            Allow explicit call of a trait methods
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Class/Object related
 Operating System:   All
 PHP Version:        5.4.3
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

Even calling static trait methods is deprecated as of PHP
8.1.0[1], so allowing to call non-static methods would not make
sense.  Use composition instead.

[1] <https://github.com/php/php-src/blob/php-8.1.0beta2/UPGRADING#L354-L356>


Previous Comments:
------------------------------------------------------------------------
[2012-06-07 18:44:15] jachym dot tousek at gmail dot com

Description:
------------
Hi,

I have a little feature request. I'd like to call a method from a used trait explicitly, even
though it was redefined in the class itself. It actually does work now, but it causes an error (see
actual result bellow) which it shouldn't in my opinion.

This should probably only work for traits used by the class itself, not for traits used by parent
classes nor traits used by traits.

I know about the "as" keyword but that's not what I want in this case. See my actual
goal at the end od the test script (in comment) for details.

Test script:
---------------
<?php

trait A {

	public function method() {
		echo 'A';
	}

}

trait B {

	public function method() {
		echo 'B';
	}

}

class C {
	use A, B;

	public function method() {
		A::method();
		B::method();
		echo 'C';
	}

}

$obj = new C;
$obj->method();

/*
My actual goal is something like this:

class D {
	use A, B;

	public function method() {
		$method = __FUNCTION__;
		foreach (class_uses($this) as $trait) {
			if (method_exists($trait, $method)) {
				$trait::$method();
			}
		}
	}

}

$obj = new D;
$obj->method();
*/

Expected result:
----------------
ABC

Actual result:
--------------
Strict Standards: Non-static method A::method() should not be called statically, assuming $this from
incompatible context in E:\localhost\www\traits.php on line 23
A
Strict Standards: Non-static method B::method() should not be called statically, assuming $this from
incompatible context in E:\localhost\www\traits.php on line 24
BC


------------------------------------------------------------------------



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


Thread (2 messages)

« previous php.bugs (#235844) next »