Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I use a library where a method returns different types depending on the arguments you pass to it, let's say:
// returns string when you pass "foo" to the method
$string = $obj->useMethod("foo");
// returns an array when you pass "bar" to the method
$array = $obj->useMethod("bar");
I'm no PHP object specialist but I have the impression that this is not really convenient (you should know in advance what is going to be returned). Is this something standard ? or should it be avoided at all ?
Surely is not very well designed, but it is perfectly legal. Even some php core methods have this behaviour (ex: parse_url can return an array if the second parameter is not set, a string if set, false if an error is encountered)
This is bad practice and should be avoided. PHP now supports type hinting and it's good practice to use them.
e.g.
class SomePaymentService implements PaymentServiceInterface
{
public function processPayment(array $parameters): array
{
// Process a payment
return [
// results
];
}
}
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Most tutorials I find online tend to use the following format:
Method 1
$functionargument = <long_argument_goes_here>;
function($functionargument);
I personally find this hard to read. I know I won't have a choice if I ever start working for a team with a style guide, but I prefer:
Method 2
function(
<long_argument_goes_here>
);
I don't know the name of these two methods, but I do know that method 1 is easier to find reference material for, and method 2 is the one I find easier to read, especially as the code gets more and more complex. If I don't see it physically nested, I forget where it came from.
One situation I've never seen in method 2 before is:
Style 1
$functionargument1 = <long_argument_goes_here>
$functionargument2 = <different_long_argument_goes_here>
function($functionargument1,$functionargument2);
I have a guess as to what the method 2 approach should be, but I'm not sure:
Style 2
function(
<long_argument_goes_here>
,
<different_long_argument_goes_here>
);
I've never seen a comma floating in the middle of code like that before, but it just feels right to me.
What is the correct name of "method 1" and "method 2"? I'd like to research this further, but I don't have the correct keywords to find it.
You could use the PSR-2 standard, there is a section about methods.
It allows you to use
public function foo($arg1, &$arg2, $arg3 = [])
{
// method body
}
and if your function names or parameter names are longer:
public function aVeryLongMethodName(
$functionargument1,
$functionargument2,
$functionargument3
) {
// method body
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I just want to know if it's a bad practise to pass null to constructor args in order to manage the non-override?
For example :
class Car
{
private $brand;
private $color;
public function __construct($brand, $color=null)
{
// ...
}
}
It is not. null is completely valid in PHP in terms of safeness (stuff like empty string or "0" or something like that is more potent to fail).
I think the correct question here is “is it bad to give default value to parameter of a contructor”, but to that question too, it is not.
My reasoning is that sometimes you want to imply default behaviour that would be used in most of the cases, and in some, say 5 % of cases, you need to alter behaviour of the object somehow, you’d want to be able to tell in the constructor that there is this kind of difference.
Sometimes, especially if the argument would make major change in the object, or if there is multiple values which are all used widely, it may more sense to use a method to set it. In you case, that method would be something like $car->color("foo");, and it would be implied to default to null.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have read articles (e.g. here and here) that suggest it harms readability to use boolean variables in function parameters. This makes sense to me; it's hard to determine what "false" means when calling a function with myFunction(false). It would be better if the argument in the function call was more descriptive. Something to indicate what is being set true/false.
What is a good way to have a boolean or boolean-like parameter in a PHP function that is more descriptive than true/false?
Assuming You have a method like this:
function fetchProducts($useCaching) { /* ... */ }
And you call it like this:
fetchProducts(true)
You could instead:
1) create two seperate methods:
fetchProducts()
fetchFreshProducts()
2) or call it like so:
fetchProducts($useCaching = true)
The second thing is useful also for other types of data. For example when you're calling a method, which accepts 2 integers as parameters. But don't do it if You aren't doing OOP paradigm. You do not want to pollute global scope with new variable names to gain some readability.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I was wondering what are the pros and cons of these 2 programming styles, is it just a matter of taste or is one the desired way?
using a method return value as an argument for another method :
public function add($data)
{
$this->uploadFilesToPreflight(
$this->addOrderFilesToDB(
$data['shop'],
$data['filesData']
)
);
}
VS setting a protected class variable in method 1 and using it as method 2 argument:
public function add($data)
{
//does some stuff and sets $orderFilesArray
$this->addOrderFilesToDB($data['shop'], $data['filesData']);
$this->uploadFilesToPreflight($this->orderFilesArray);
}
The question is, what is the class supposed to be? And does the class have a state?
If the class is supposed to be a service where add($data) can be called multiply times and the behavior should not change the first option would be better.
If the class has a state and it's behavior is supposed to change by call add($data) then the second option is fine but you have to be aware that this can cause side-effects if the class has other methods that rely on $this->orderFilesArray.
To provide a better answer on what option is best, you would have to post a broader context of the class.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a pretty basic question on whether or not what I'm doing is a good practice, or is overkill.
class myClass
{
public function doSomething($thing=false)
{
if (!$thing) echo "Your thing is missing!";
echo "Did you get that $thing I sentcha?";
}
}
Should every function have these precautions, or only those that could be likely to be mis-used (though, I suppose finding those functions is subjective).
Is it a good practice to set function arguments to false (assuming they should normally carry non-falsy values)? This also prevents other developers from mis-using the functions, mainly if they forget to enter a value.
Or ... Is this a waste/overkill if I assume developers using this function will use it correctly and not leave out $thing?
don't do that, a function with a pre-set parameter value is basically an invitation to NOT pass an argument. Especially if you're using an IDE with auto-complete...
PHP will complain if the method is called with a missing parameter, and you should actively check if the parameter is valid and if this is not the case, throw an exception.
As a general rule, try to extend your validation so that the actual format (number, string, percentage, currency, ....) is validated instead of just checking if "something" is there ;)