03-05-2007, 16:39
|
|
|
|
חבר מתאריך: 05.08.06
הודעות: 2,860
|
|
לא מומלץ להשתמש במשתנים גלובליים באף שפה(כולל php), כאשר ניתן להימנע מהם (ופה ניתן)
משתנים גלובליים מגדילים את התלות בין מרכיבי התוכנה, מקשים על מעקב אחר התכנית וגורמים לעוד בעיות בטווח הרחוק (אתה מוזמן לקרוא את הציטוט מלמטה ולחפש באינטרנט עבור עוד מידע)
כדי לחסוך בהסברים אני אשים כמה ציטוטים מויקיפדיה :
ציטוט:
They are usually considered bad practice precisely because of their nonlocality: a global variable can potentially be modified from anywhere, and any part of the program may depend on it. A global variable therefore has an unlimited potential for creating mutual dependencies, and adding mutual dependencies increases complexity.
....
The use of global variables makes software harder to read and understand. Since any code anywhere in the program can change the value of the variable at any time, understanding the use of the variable may entail understanding a large portion of the
program.
...
The setting of a global variable can create side effects that are hard to understand and predict. The use of globals make it more difficult to isolate units of code for purposes of unit testing, thus they can directly contribute to lowering the quality of the code.
|
והטענה שלך "אולי בc אבל לא בphp" היא טפשית. אם כבר אז בc לעתים יש הצדקה לשימוש במשתנים גלובליים, בעוד שבphp אף פעם אין הצדקה כזאת.
|