How do I write a function with two long arguments? [closed] - php

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
}

Related

Method delivering different types [closed]

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
];
}
}

Is it bad practice to pass null to a constructor args in PHP? [closed]

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.

Improve readability of boolean parameter in PHP function [closed]

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.

basic php classes, error catching for arguments [closed]

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 ;)

My colleague writes code on PHP, and uses as a constant ... attention - static methods [closed]

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.e. instead of:
const MY_CONST = 0;
he writes:
public static function MY_CONST() { return 0 };
My former colleagues, front-end developers, doing so in JavaScript, because there are no constants, but to do so in PHP? In my opinion, if the language have a constants, you should use them.
I want to send him a link to this question, as my arguments.
Writing method to return always same value is senseless. Of course it is better pratice to use constans. Even when you look at perfomace it's faster when you declare something that you know won't change as constant.
I agree with hakre don't argue with idiots they will bring you to their level and beat with experience.
I read somewhere that in PHP constans are not so fast. You should read this article.
http://planetozh.com/blog/2006/06/php-variables-vs-constants/
In my opinion, if the language have a constants, you should use them.
Sure, that's why they are in. Otherwise it is not a constant. Did he said he needs a constant? If so, tell him there is a PHP manual explaining it in case the language is new to him.
The URLs are http://php.net/const and http://php.net/constants .
Apart from these fundamental and bare things, do not argue with idiots. They live - by the original meaning of the word - in their own world. So you can not get through to them.

Categories