Why does PHP allow you to overload pointers to constructors, but not to methods?

Suppose you have this:

<?php

interface Type
{}

class SomeType implements Type
{}

class SomeOtherType implements Type
{}

Now, if you have an abstract class with an interface Typeas a constructor dependency, for example:

<?php

abstract class TypeUser
{
        public function __construct(Type $type)
        {}
}

Why PHP allows you to overload the constructor on specific classes as follows:

<?php

class SomeTypeUser extends TypeUser
{
        public function __construct(SomeType $type)
        {}
}

class SomeOtherTypeUser extends TypeUser
{
        public function __construct(SomeOtherType $type)
        {}
}

But this does not allow you to do the same for methods, only constructors - if you try to do this using non-constructor methods, does PHP throw an error Strict standards?

+4
source share
2 answers

Read the following: Change a method of a method with a child interface as a new parameter

TL; DR: , . TypeUser , , , , , .

? () :

$user = new SomeTypeUser($parameter);

, , , .

:

function (TypeUser $user) {
    $user->foo($something);
}

, $user ? SomeTypeUser? SomeOtherTypeUser? - ? $user->foo() foo(SomeType $type) foo(SomeOtherType $type)? ? ...

0

PHP , abstract interface, /.

interface Foo {
    public function baz(ArrayObject $array);
}

class Bar implements Foo {
   public function baz(DateTime $dateTime) {
   }
}

abstract class Foo {
    public abstract function baz(ArrayObject $array);
}

class Bar extends Foo {
   public function baz(DateTime $dateTime) {
   }
}

:

: Bar:: baz() Foo:: baz ( ArrayObject $) /.../test.php xy

abstract class Foo {
    public function baz(ArrayObject $array) {
    }
}

class Bar extends Foo {
   public function baz(DateTime $dateTime) {
   }
}

, baz .

-1

All Articles