xref: /PHP-8.2/Zend/tests/traits/constant_001.phpt (revision 3b62d660)
1--TEST--
2Trying to access a constant on Trait via a Class
3--FILE--
4<?php
5
6trait Foo {
7    public const PUBLIC = 'public';
8    protected const PROTECTED = 'protected';
9    private const PRIVATE = 'private';
10
11    public function f1(): void {
12        echo self::PUBLIC, ' via self', PHP_EOL;
13        echo static::PUBLIC, ' via static', PHP_EOL;
14        echo $this::PUBLIC, ' via $this', PHP_EOL;
15    }
16}
17
18class Base {
19    use Foo;
20
21    public function f2(): void {
22        echo self::PRIVATE, ' via self', PHP_EOL;
23        echo static::PRIVATE, ' via static', PHP_EOL;
24    }
25}
26
27class Derived extends Base {
28    public function f3(): void {
29        echo self::PROTECTED, ' via self', PHP_EOL;
30        echo static::PROTECTED, ' via static', PHP_EOL;
31        echo parent::PROTECTED, ' via parent', PHP_EOL;
32    }
33}
34
35echo Base::PUBLIC, ' via class name', PHP_EOL;
36echo (new Base)::PUBLIC, ' via object', PHP_EOL;
37(new Base)->f1();
38(new Base)->f2();
39echo Derived::PUBLIC, ' via derived class name', PHP_EOL;
40echo (new Derived)::PUBLIC, ' via derived class object', PHP_EOL;
41(new Derived)->f3();
42?>
43--EXPECTF--
44public via class name
45public via object
46public via self
47public via static
48public via $this
49private via self
50private via static
51public via derived class name
52public via derived class object
53protected via self
54protected via static
55protected via parent
56