svn: /phpdoc/en/trunk/appendices/ userlandnaming.xml

From: Date: Fri, 15 Jul 2016 18:24:17 +0000
Subject: svn: /phpdoc/en/trunk/appendices/ userlandnaming.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-14342@lists.php.net to get a copy of this message
peehaa Fri, 15 Jul 2016 18:24:17 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=339672 Log: Made the tips actually somewhat useful instead of wtf Changed paths: U phpdoc/en/trunk/appendices/userlandnaming.xml Modified: phpdoc/en/trunk/appendices/userlandnaming.xml =================================================================== --- phpdoc/en/trunk/appendices/userlandnaming.xml 2016-07-15 18:08:07 UTC (rev 339671) +++ phpdoc/en/trunk/appendices/userlandnaming.xml 2016-07-15 18:24:17 UTC (rev 339672) @@ -99,20 +99,49 @@ <section xml:id="userlandnaming.tips"> <title>Tips</title> <para> - In order to write future-proof code, it is recommended that you prefix - (or suffix) anything that goes into the global namespace with an uncommon - 3-4 letter prefix (or suffix) separated with an underscore. It is - recommended that in order to prevent namespace clashes with other userland - code that projects research existing prefixes (or suffixes) used in other - projects and advertise their chosen prefix (or suffix) appropriately. - Examples: + In order to write future-proof code, it is recommended that you don't + place many variables, functions or classes in the global namespace. This will + prevent naming conflicts with 3rd party code as well as possible + future additions to the language. </para> + <para> + One common way to prevent naming conflicts of functions and classes is to + add them to their own dedicated + <link linkend="language.namespaces">namespace</link>. + </para> + <informalexample> + <programlisting role="php"> +<![CDATA[ +<?php - <itemizedlist> - <listitem><para>MyPx_someFunc()</para></listitem> - <listitem><para>Foo_Date</para></listitem> - <listitem><para>$asdf_dbh</para></listitem> - </itemizedlist> +namespace MyProject; + +function my_function() { + return true; +} + +\MyProject\my_function(); +]]> + </programlisting> + </informalexample> + <para> + This still needs you to keep track of already used namespaces, but once you + have decided on a namespace you will be using you can add all functions and + classes to it without having to think about conflicts again. + </para> + <para> + It is considered best practice to limit the number of variables added to + the global scope in order to prevent naming conflicts with 3rd party + code. + </para> + <note> + <title>Variable scoping</title> + <para> + Because of PHP's <link linkend="language.variables.scope">scoping rules</link> + variables defined inside function or methods are not in the global scope + and as such cannot conflict with other variables defined in the global scope. + </para> + </note> </section> </appendix>

« previous php.doc.cvs (#14342) next »