Answer the question
In order to leave comments, you need to log in
What is the best way to implement transition logic in a router?
We need logic for such paths:
1) https://sitename/city/product/
2) https://sitename/city/
3) https://sitename/product/
Answer the question
In order to leave comments, you need to log in
{
path: '/city/:product'
},
{
path: '/:cityOrProduct'
},
I would create an AbstractClass class so that CommonClass and ConcreteClass1 inherit from it. ConcreteClass2 must inherit from ConcreteClass1. In AbstractClass, I would take out from CommonClass those members that should be common to all classes. Everything.
No way, either inheritance or traits.
The most optimal variant of the trait.
Maybe like this:
common.php:
public function doSomething1() {/*...*/}
public function doSomething2() {/*...*/}
class CommonClass {
public function foo() {/*...*/}
public function bar() {/*...*/}
}
class ConcreteClass1 extends CommonClass {
include "common.php";
}
class ConcreteClass2 {
include "common.php";
public function doSomething3() {/*...*/}
}
Delegation usually gives more flexibility than inheritance.
class CommonClass {
public function foo() {/*...*/}
public function bar() {/*...*/}
}
class ConcreteClass1 extends CommonClass {
public function doSomething1() {/*...*/}
public function doSomething2() {/*...*/}
}
class ConcreteClass2 {
ConcreteClass1 concreteClass1 = new ConcreteClass1();
public function doSomething1() {concreteClass1.doSomething1()}
public function doSomething2() {concreteClass1.doSomething2()}
public function doSomething3() {/*...*/}
}
I support the delegation idea. Another would be to spread the logic across different interfaces
interface Common {
void foo();
void bar();
}
interface ConcreteWithSomething {
void doSomething1();
void doSomething2();
}
interface Concrete1 extends Common, ConcreteWithSomething {}
interface Concrete2 extends ConcreteWithSomething {
void doSomething3();
}
class Concrete2Impl implements Concrete2 {
@Inject private Concrete1 delegate;
public void doSomething1() { delegate.doSomething1(); }
public void doSomething2() { delegate.doSomething2(); }
public void doSomething3() {/*...*/}
}
therefore, in ConcreteClass2, the methods foo() and bar() from CommonClass should not be.
public function foo() { return null; }
public function doSomething1() {
$cl = new ConcreteClass1();
return $cl->doSomething1();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question