English 中文(简体)
消除工厂方法:如何归还儿童类别物体?
原标题:Overriding of factory methods: how to return the child class object type?

请允许我指出,我拥有像现在这样一位家长:

class A {

    private $property;

    public static function factory($arg) {
        $object = new A();
        $object->property = $arg;
        return $object;
    }

}

我愿以这种方式延长:

class B extends A {

    public static function factory() {
        return parent::factory( constant );
    }

}

当I do B:因果() 我收到了<代码>A的物体。 如果我想要的是哪一种类型的标的? B。 <<>tong>I 不能改动A

最佳回答

1st version

That s because you hardcoded the A class in the factory method.
In class A, instead of $object = new A() try (require Php 5.3):

$class_name = get_called_class();
$object = new $class_name;

get_called_class() "Gets the name of the class the static method is called in."

较短版本:

$object = new static();

2nd version (hardcoded parent class):

人工授精:

$parent = parent::factory($args);
$obj = new static();
$obj->setTimestamp($parent->getTimestamp());
$obj->setTimezone($parent->getTimezone());
return $obj;

或者使用黑板自动进行:

如何对待PHP中的 Cast

问题回答

例如:

  1. You have two classes (unleast)
  2. both classes can be instantiated (concrete, not pure or abstract)
  3. one class is a superclass of another
  4. both classes are instantiated with a "factory" method
  5. the "factory" method of a subclass can invoke the the "factory" method of the superclass
  6. each "factory" method can have several type or count of parameters

<><>Problem>

我现在要注意:

class B extends A {

    public static function factory() {
        return parent::factory( constant );
    }

}

www.un.org/spanish/ecosoc 简讯和快速回答:

修改如下:

class B extends A {

    public static function factory() {
        return A::factory( constant );
    }

}

http://www.un.org。

你们试图压倒(和超载,有不同的参数)固定功能。

其共同错误假定固定方法是虚拟的,可以推翻。 有些方案语言允许(Object Pascal, Delphi),其他非专利(C#, Java),PHP取决于版本,我想。

为了诚实起见,“法定职能”的工作类似于具有名称空间的全球职能,可公开接触某类成员,而不是使用方法。 我建议将这些职能视为全球职能,始终如此。

Cheers.





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签