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 know what the use operator is doing in something like function($x,$y) use ($z) { ...
What I don't understand is why PHP uses this construction when other languages don't?
Javascript has rather loose variable scoping (you don't need to declare variables as global). PHP has tighter variable scoping (if a variable isn't defined within the scope that it's used, and isn't brought in with global, then it doesn't exist).
The use declaration tells PHP to make those variables available within the closure (and likely also tells the garbage collector not to clean them up until after the closure gets cleaned up).
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 2 years ago.
Improve this question
I read somewhere that defining constants with PHP's define function like so:
define('BASE_PATH','/var/www/html/example/');
is better and more secure than storing the same variable data inside a globals variable like so:
$_GLOBALS['BASE_PATH'] = '/var/www/html/example/';
Could somebody please explain the difference, which is better in which scenarios, and why?
I've just read here:
PHP Manual - The 'define' function
That the 'define' function can cause unwanted oddities.
a) What are the security implications of both?
b) How does PHP manage and store each of the variable's data in physical memory?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've been using PHP for some basics Back-End development for a while now. I saw something about interpretations of variables while I was looking for some changes which came with PHP 7. I'm not using them and it would be great if someone can explain why to use them?
What I mean is:
What are the pros of using them?
You use variable interpretation in situations when you need to dynamically reference a variable and don't want to use an array. Generally I would not recommend using it, as you lose benefits such as static code analysis.
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 7 years ago.
Improve this question
If during a function, I new AnObject - do I have to unset it before the function exist? or is it done automatically using reference counting?
The garbage collection should take care of it for you:
PHP performs garbage collection at three primary junctures:
When you tell it to
When you leave a function
When the script ends
So no, you should not have to unset anything, but you can if you want to.
When unsettling a variable it seems that you only are marking the value for the garbage collector. And when you're exiting your function's scope, the reference counter will be decremented, and your object will be marked to be deleted, having the same result that you would have calling the unset.
https://php.net/manual/en/features.gc.refcounting-basics.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 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.