Jump to content

Recommended PHP style guide?


MarcC

Recommended Posts

Not sure what you mean. I just put all my scripts inside  /site/templates/scripts/

Sorry, I probably wasn't very clear on that - I am talking about php scripts that need to be called from the browser, but that are not PW template files. For example this could be an AJAX call to a php file.

That make sense now?

To clarify, any javascript files can definitely be run from inside templates/scripts.

Sorry for the confusion.

Link to comment
Share on other sites

Guys, thanks very much for the very quick responses. I get now why there is no predefined way to organize things, and understanding that will help. I think the rest will come with time. Guess I am just not used to having this much control.

You have all been very helpful.

Thanks again.

Link to comment
Share on other sites

It might be worth you just doing one of the easy tutorials - they are a bit like being taught to suck eggs (sorry, I tend to write like that) but they will give you a good idea of how to think your way through the process.

Again, they are just one way of doing things, but they are very clear.

http://wiki.processwire.com/index.php/Category:Long_Tutorials

The basic website tutorial is as a good a place as any.

Link to comment
Share on other sites

Oh, one little tip if you like using JQuery plugins.

The default PW installation comes with Styles and Scripts folders inside of templates. However, a lot of frameworks and JS plugins use css and js as folder names.

When I start a new PW project, the first thing I do is change the names to css and js - I get less puzzling surprises that way!

  • Like 2
Link to comment
Share on other sites

Guess I am just not used to having this much control.

Exactly my own thoughts when I first discovered PW. 'It can't be that easy.' (I came from a mostly Mambo/Joomla & Wordpress background and found it unbelievable that a CMS existed that didn't enforce rigid field & template structures on the user.)

  • Like 3
Link to comment
Share on other sites

Guess I am just not used to having this much control.

That's what I am still struggling with. Having worked before with a lot of other cms'es and wysiwygets

and then all of a sudden all the rules and walls fall away. You have to step over old thinking, habits

and routines. There's also self coding and php that gets in what you need to learn.

  • Like 3
Link to comment
Share on other sites

  • 5 months later...
  • 1 year later...

The style guide is here:

https://processwire.com/api/coding-style-guide/

However, I don't get why are double quotes recommended for strings echoed here:

https://processwire.com/api/coding-style-guide/#8.1-single-vs-double-quotes

Doing so will result in such html:

<link rel='stylesheet' media='screen' href='style.css'>

Which is perfectly valid but in most cases double quotes are used for html tags - at least I think so.

Following the style guide will likely result in mixed html quotes on a site. I also see that variables may be easier to add if the string is encapsulated in double quotes. However, editors and IDEs can higlight variables better if they are concatenated, and they are easier to spot.

Just asking.

Link to comment
Share on other sites

Editors like PHPStorm (which I know a few here use) will highlight variables the same however you use quotes, so shouldn't be any harder to spot.

I know Ryan uses PHPStorm and so do several others here but not sure if other editors cause variables to be less noticeable?

  • Like 1
Link to comment
Share on other sites

There are a few advantages using double quotes for strings. It's not uncommon that sentences have single quotes like this sentence. When sticking to double quotes it is more unlikely that you have to change your quoting style and get mixed quotings styles. Next variables are processed in double quotes.

I prefer writing PHP containing HTML above HTML containing PHP. So the single quote HTML tags makes perfectly sense to me.

  • Like 1
Link to comment
Share on other sites

I see your points, thanks.

I also use PhpStorm and yes, variables are highlighted in such cases too.

Consider this:

$editUrl = "data-mfp-src='{$page->editUrl}$lang&modal=1'";
$editUrl = 'data-mfp-src="' . $page->editUrl . $lang . '&modal=1"';

post-3156-0-37926300-1448569420_thumb.pn

The first will result in single quote html tags and it is less readable imho.

The second results in double quotes and for me it's more readable and separated.

The first will most likely cause mixed quotes in html tags if you view the whole site's source. I know it's not a big deal but it doesn't seem logical to me. One should write single quotes for html tags in all his template files to avoid mixed quotes.

Of course this is only my opinion, perhaps because I'm coming from the front-end side.

Link to comment
Share on other sites

It is RECOMMENDED that you use variables embedded in strings, when possible, rather than concatenating multiple strings. Double quotes must be used to embed variables in strings.

It is a recommendation (and for good reason...performance-wise [Google it] :-)).....but, personally, I almost always use your '2nd' example 

$editUrl = 'data-mfp-src="' . $page->editUrl . $lang . '&modal=1"';

...for readability.... as long as there is no performance hit... :rolleyes:  :)

  • Like 1
Link to comment
Share on other sites

I know it's only a recommendation but wanted to know if there's a reason that I'm missing. (seems no)

I've set up the PHP coding style in PhpStorm according to the style guide and almost everything was similar as my personal preference, except the quotes (partly).

Link to comment
Share on other sites

Html does not care about which quotes are used and from the php standpoint it's more useful to go with double quotes to have the ability to insert variables directly. If that doesn't fit your style you can still use explicit concatination. Html 5 doesn't even need quote in lot's of arguments anymore and I really cannot remember the last time I actually looked at raw source code. All the dev tools automatically convert quotes to the double ones so I'm not sure why inconsistancy in the html result would matter.

Edit:

Not to mention, that this style guide is meant for the core development, where probably 80% of the code aren't related to html.

Link to comment
Share on other sites

The first will result in single quote html tags and it is less readable imho.

The second results in double quotes and for me it's more readable and separated.

The first will most likely cause mixed quotes in html tags if you view the whole site's source. I know it's not a big deal but it doesn't seem logical to me. One should write single quotes for html tags in all his template files to avoid mixed quotes.

Your preference towards double quotes in HTML markup is purely visual. The browser doesn't care, users won't care, and if you enable minification via something like ProCache, it's actually possible that it might automatically remove some of those quotes altogether. In many cases the quotes are not needed at all, which means that they're just taking extra space.

Regardless, my personal preference in PHP is to always use single quotes by default, and double quotes only if I really want to embed variables in the string. Using single quotes means that there's zero chance of accidentally inserting a variable, or really any string starting with dollar sign that would be interpreted as one.

When I do want variables in the string, I prefer double quotes, though I also tend to use curly braces everywhere, regardless of whether they're "necessary":

$editUrl = "data-mfp-src='{$page->editUrl}{$lang}&modal=1'";

Regarding editors: my opinion is that any decent editor should get highlighting right. If your preferred editor doesn't, consider switching. If syntax highlighting works in some cases, but not always, that's a potential source of confusion.

In the end the style guide for ProcessWire is no different from other style guides in that many things there are opinion-based. If you don't want to use that in your own projects, you don't have to, but of course adhering to it could make working together with fellow ProcessWire users slightly easier.  :)

Link to comment
Share on other sites

one of the best resource on this topic:

http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html

general more simple stats on what PHP handling has impact on speed:

http://phpbench.com/

regards mr-fan

...'i'.'like'.'this'.'style'.'too'.'but'.'it'.'seems'.'that'.'in'.'fact'.'it'.'is'.'slower'....;)

  • Like 3
Link to comment
Share on other sites

  • 7 years later...

Time to revive this 8 year old thread 🙂

I'm using vscodium as editor and recently for code formatting I moved from the excellent https://intelephense.com/ extension to an extension that uses https://github.com/PHP-CS-Fixer/PHP-CS-Fixer which is based on https://github.com/squizlabs/PHP_CodeSniffer. This allows custom style definitions (rulesets) https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file

PW coding style guide says:

Quote

The ProcessWire coding style guide is based on PSR-1 and PSR-2 (with many sections copied directly from them), but with several important differences and additions.

Has anybody ever gone through the trouble of creating a custom PW ruleset.xml? Just asking before I start spending my time on that. Other CMSs/Frameworks do have their ruleset files. See
https://github.com/WordPress/WordPress-Coding-Standards 
https://www.drupal.org/docs/contributed-modules/code-review-module/installing-coder-sniffer 
https://github.com/fossbarrow/laravel-phpcs 

Would be great if we had one for ProcessWire, too. Maybe @ryan  already uses one and can share it? If not, we could start a community effort to produce such ruleset. It would help tremendously for PRs to the PW core or for module development.

  • Like 3
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...