I have two functions: myFunctionA()
and myFunctionB()
.
myFunctionA()
returns an Object
which includes the key Page_Type
which has a string value.
myFunctionB()
processes a number of entries in the Object
returned by myFunctionA()
, including Page_Type
and its string value.
Later, myFunctionA()
is updated so it no longer returns an object including the key Page_Type
but, instead, the key Page_Types
- which has an array value.
Because of this, myFunctionB()
will now also need to be updated - it will no longer be processing Page_Type
which is a string, but Page_Types
which is an array.
If I understand correctly (and I may not), the above is an example of Dependency Request and the extensive refactoring that it throws up can be avoided by (I think) deploying the Dependency Injection pattern (or possibly even the Service Locator pattern?) instead.
But, despite reading around this subject, I am still uncertain as to how Dependency Injection can work in PHP or Javascript functions (much of the explanation deals with programming languages like C++
and OOP concepts like Classes
, whereas I am dealing with third party functions in PHP and javascript).
Is there any way to structure my code so that updating myFunctionA()
(in any significant manner) will not then require me to also update myFunctionB()
(and all other functions calling myFunctionA()
- for instance myFunctionC()
, myFunctionD()
, myFunctionE()
etc.) ?
And what if myFunctionH()
requires myFunctionG()
requires myFunctionF()
which requires myFunctionA()
? I don't want to be in a position where updating myFunctionA()
now means that three more functions (F
, G
and H
) all have to be updated.
Attempt at an answer:
The best answer I can think of at present - and this may not be a best practice answer because I don't know yet if there is a formal problem which corresponds with the problem I am describing in the question above - is the following restatement of how I presented the setup:
I have two (immutable) functions:
myFunctionA__v1_0()
andmyFunctionB__v1_0()
.
myFunctionA__v1_0()
returns an Object which includes the keyPage_Type
which has a string value.
myFunctionB__v1_0()
processes a number of entries in the Object returned bymyFunctionA__v1_0()
, includingPage_Type
and its string value.Later,
myFunctionA__v1_0()
still exists but is also succeeded bymyFunctionA__v2_0()
which returns an object including the keyPage_Types
- which has an array value.In order for
myFunctionB
to access the object returned bymyFunctionA__v2_0()
, there will now also need to be amyFunctionB__v1_1()
, capable of processing the arrayPage_Types
.
This can be summarised as:
myFunctionB__v1_0()
requires object returned bymyFunctionA__v1_0()
myFunctionB__v1_1()
requires object returned bymyFunctionA__v2_0()
Since each function becomes immutable after being formally named, what never happens is that we end up with an example of myFunctionB__v1_0()
requiring object returned by myFunctionA__v2_0()
.
I don't know if I am approaching this the right way, but this is the best approach I have come up with so far.
Aucun commentaire:
Enregistrer un commentaire