从父类创建对象并从子类调用类

时间:2021-07-18 16:08:06

I have one class created but is huge and has to many methodes. I want to slice to separate child classes.

我创建了一个类,但是很大,并且有许多方法。我想切片分开子类。

I attempt something like this:

我尝试这样的事情:


class ParentClass{

}

class ChildClass1 extends ParentClass{

      public function childFunction1(){

      }

}

class ChildClass2 extends ParentClass{

      public function childFunction2(){

      }

}

$myObject = new ParentClass();
$myObject->childFunction1();
$myObject->childFunction2();


But not working.
It's that possible?
Is there any alternative solution?

3 个解决方案

#1


1  

No, it will not work that way. What you're doing is instantiating an empty class and trying to call methods on it.

不,它不会那样工作。你正在做的是实例化一个空类并尝试在其上调用方法。

You should use composition instead. The basic idea is to compose multiple objects into your main object. Break off objects by their functionality. Always remember to follow the Single Responsibility Principle and the rest of SOLID.

你应该使用构图。基本思想是将多个对象组合到主对象中。通过功能中断对象。永远记住遵循单一责任原则和SOLID的其余部分。

#2


0  

class ParentClass {

}


class ChildClass1 extends ParentClass{

    public function childFunction1(){

    }

}

class ChildClass2 extends ParentClass{

    public function childFunction2(){

    }

}

$myObject1 = new ChildClass1();
$myObject2 = new ChildClass2();
$myObject1->childFunction1();
$myObject2->childFunction2();

I feel it is like this

我觉得这是这样的

#3


0  

I've found solution alone.

我找到了解决方案。

class ParentClass{

      public function childFunctions($className,$functionName){
           $c = ucfirst($className);
           $$className = neew $c();
           return $$className->$finctionName(); // or echo
      }

}

class ChildClass1 extends ParentClass{

      public function childFunction1(){

      }

}

class ChildClass2 extends ParentClass{

      public function childFunction2(){

      }

}
$myObject = new ParentClass();
$myObject->childFunctions("ChildClass1", "childFunction1");
$myObject->childFunctions("ChildClass2", "childFunction2");

That work for my problem. Maybe someone has the same problem.

这对我的问题有用。也许有人有同样的问题。

Thank you anyway.

不管怎样,谢谢你。

#1


1  

No, it will not work that way. What you're doing is instantiating an empty class and trying to call methods on it.

不,它不会那样工作。你正在做的是实例化一个空类并尝试在其上调用方法。

You should use composition instead. The basic idea is to compose multiple objects into your main object. Break off objects by their functionality. Always remember to follow the Single Responsibility Principle and the rest of SOLID.

你应该使用构图。基本思想是将多个对象组合到主对象中。通过功能中断对象。永远记住遵循单一责任原则和SOLID的其余部分。

#2


0  

class ParentClass {

}


class ChildClass1 extends ParentClass{

    public function childFunction1(){

    }

}

class ChildClass2 extends ParentClass{

    public function childFunction2(){

    }

}

$myObject1 = new ChildClass1();
$myObject2 = new ChildClass2();
$myObject1->childFunction1();
$myObject2->childFunction2();

I feel it is like this

我觉得这是这样的

#3


0  

I've found solution alone.

我找到了解决方案。

class ParentClass{

      public function childFunctions($className,$functionName){
           $c = ucfirst($className);
           $$className = neew $c();
           return $$className->$finctionName(); // or echo
      }

}

class ChildClass1 extends ParentClass{

      public function childFunction1(){

      }

}

class ChildClass2 extends ParentClass{

      public function childFunction2(){

      }

}
$myObject = new ParentClass();
$myObject->childFunctions("ChildClass1", "childFunction1");
$myObject->childFunctions("ChildClass2", "childFunction2");

That work for my problem. Maybe someone has the same problem.

这对我的问题有用。也许有人有同样的问题。

Thank you anyway.

不管怎样,谢谢你。