Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Tim MacDonald:
Loading Eloquent relationship counts
Nov 12, 2018 @ 15:51:30

Tim MacDonald has a new post to his site sharing methods that the Laravel Eloquent users (either in the framework or outside of it) can use to load in the counts of relationships without having to fetch the entire relationship data set.

It is often useful to show the number of related models a given instance has, but not actually need any specific information about the related models, just how many exist. In this scenario you do not want to load all of the related models into memory to count them, we want our database to do the heavy lifting for us. Laravel offers a number of ways to retrieve relationship counts. 2 have been around for a while, but there is a new kid on the block.

He looks at three methods you can use to get these counts: via the query builder manually, directly on the relationship and, more recently added, from an eloquent collection. He goes through each of these methods, providing a summary of the technique and code examples showing how it's implemented.

tagged: laravel eloquent relationship count tutorial querybuilder collection

Link: https://timacdonald.me/loading-eloquent-relationship-counts/

Matt Stauffer:
How to count the number of lines of code in a PHP project
Jun 25, 2018 @ 15:50:37

In this post to his site Matt Stauffer shares several methods you can use to count the total number of lines of code in a PHP project.

I'm giving a talk soon about Laravel and "the enterprise", and the concept of LOC (lines of code) keeps coming up. It turns out that's actually a much harder number to discover than you might think, so I figured I would write up a few options here.

For what it's worth, I'm not a big fan of LOC as a measure of any importance, but it can at least give us some broad foundations to use to talk about broad differences in project size. If you were to ask me, I would say we shouldn't even think about it. But we don't always have that luxury.

He starts with a tl;dr for those that want the quick win (use PHPLOC) but shares other options with different tools too:

He shares answers to a few FAQs about finding lines of code and the output of each tool/command on the same project to show the differences.

tagged: linesofcode code lines count tutorial phploc cloc phpstorm sublimetext silversearcher

Link: https://mattstauffer.com/blog/how-to-count-the-number-of-lines-of-code-in-a-php-project/

Exakat Blog:
How many parameters is too many?
May 01, 2018 @ 16:55:47

In a new post to the Exakat blog they try to answer the question "how many parameters is too many" when it comes to the structure of the methods and functions in your application.

Now, that is a classic question, that is often a minefield for anyone writing an increasing long list of argument in a method, or simply trying to set up auditing tools.

Obviously, the answer is not immediate. Parameters may be needed, but on the other hands, currying functions allows to reduce the amount of parameter to one for every function. In between, probably exists a reasonable level that is a golden rule, and also very elusive. So, we decided to check the current practice in PHP code.

They started the research with some of PHP's own native functions that took in specific arguments, ignoring those that took an arbitrary number. Next they made a survey of 1900 open source projects to determine the common practice for parameters by function. The results showed that methods without at least one parameter were "less useful" and that a seemingly reasonable amount of parameters is 5. The post finishes with a spotlight of two they found during their research that had the most parameters: a generated class for database interaction and a dependency injection class.

tagged: parameters count statistics userland native function method results

Link: https://www.exakat.io/how-many-parameters-is-too-many/

Joshua Thjissen:
Benford’s law in frameworks
Dec 10, 2015 @ 17:10:50

Joshua Thijssen has an interesting post to his site talking about Benford's Law, related to digits and how frequently they would appear in results based on significance.

In a new talk I’m currently presenting at conferences and meetups, I talk – amongst other things – about Benford’s law. This law states that in natural occurring numbers, the first digit of those numbers will most often start with a 1 (around 30% of the time), and logarithmically drops down to the number 9, which occurs only 5% of the time.

[...] Even though there is no guarantee that something will actually follow Benford’s law, a lot of things do, and in fact, it can be used for things like fraud detection: in your taxes, in elections, but basically anything concerning numbers. [...] But anyway, I wanted to see Benford’s law in action for myself, so I’ve come up with a simple test: Take a (PHP) framework, and count the line-numbers for each PHP file in the framework.

He shares the script (well, command) he uses to get these counts and how he did the sorting to help make some sense out of the results. He includes some of the results and graphs showing them to help visualize the Benford’s "curve" the results take. Interestingly enough, most of them follow the trend very closely with only slight variances for Zend Framework v2 and only them because it fluctuates more, nothing to do with the quality of the framework.

tagged: benfordslaw trend line count framework graph results

Link: https://www.adayinthelifeof.nl/2015/12/09/benfords-law-in-frameworks/

Developer Drive:
How to build an auto-ranking Twitter list with WordPress
Oct 23, 2015 @ 18:55:36

On the Developer Drive site there's a tutorial posted showing how to create a dynamic auto-ranking Twitter list in a WordPress-based application.

My team and I recently built an awesome list template on WordPress that ranks a set of Twitter users based on follower count. It allows a content writer to easily add a list of Twitter handles, and generate a well designed post.

They start with a list of requirements the end result needs to meet including the Twitter information, features it should offer and the resulting output. The rest of the post walks you through every step of the process to get the system set up including:

  • installing the Advanced Custom Fields Pro WordPress plugin
  • Showing an "infinite list" in WordPress
  • Code to loop through the Twitter data
  • using the TwitterAPIExchange PHP library to get Twitter data

All code and steps you'll need to make the system work are included and they've posted a demo so you can see the result first hand.

tagged: autorank wordpress list twitter follower count api interface tutorial

Link: http://www.developerdrive.com/2015/10/how-to-build-an-auto-ranking-twitter-list-with-wordpress/

Joshua Thijssen's Blog:
SPL: Using the iteratorAggregate interface
Dec 06, 2011 @ 14:28:45

Joshua Thijssen has a recent post spotlighting a part of the Standard PHP Library (SPL) that implements that Traversable interface, the IteratorAggregate interface.

Together with its more famous brother "Iterator", they are currently the two only implementations of the "Traversable" interface, which is needed for objects so they can be used within a standard foreach() loop. But why and when should we use the iteratorAggregate?

He answers his question with an example - a book that contains chapters. With a normal iterator you'd have to define standard functions (like valid, rewind or key). Using the IteratorAggregate you can push items into an internal array (like chapters in a book) and call a "getIterator" method to get this set. He also takes it one step further and shows implementing the "Count" interface to make it easier to get a total count of the items in the iterator. Sample code is included to help clarify.

tagged: spl iteratoraggregate interface traversable count

Link:

Havard Eide's Blog:
Countable
Aug 01, 2008 @ 15:23:28

In a new post Havard Eide looks at the creation of a Countable interface that can be used in any application:

Today I will look at the Countable interface, it has a single function that needs to be implemented: count(), by implementing this you can ensure that there is a count() function ready to use on any given class that implements it. The Countable interface is used in other places in the SPL as well: the ArrayIterator and ArrayObject classes implements this interface ( and SqliteResult if present ).

In his code examples he shows simple methods for returning the count() of a property, but notes that the real power of it comes in the ability to manipulate the number returned from the call based on other parameters (or filtering).

tagged: countable interface count spl arrayiterator arrayobject

Link:

Developer Tutorials Blog:
Building Web 2.0 Tag Clouds in PHP
May 02, 2008 @ 18:15:27

The Developer Tutorials blog has posted a tutorial showing you how to set up a tag cloud for your site:

Every major website seems to have a tag cloud. Users love tag clouds; they help navigate masses of content quickly and easily. [...] How do we actually build a tag cloud at application level? In this tutorial, I'll take you through putting together a full-blown, calculated web 2.0 tag cloud in PHP.

They start at the end, with an example cloud with some sample tags ("php" being the largest, of course). The code for it is pretty simple - it looks at an array of counts, the number of times the tag is used, and builds the links with the correct font size automatically.

tagged: tag cloud tutorial automatic count

Link:

The Bakery:
Checking for duplicate records (unique record)
Jan 23, 2007 @ 21:33:00

On The Bakery, there's a new expanded tutorial (from this) that talks about how to check for duplicate records in your CakePHP model.

[Here's how to] validate a form field (such as a user name field), both in add and edit form and make sure that the selected user name does not already exist in the database [via a] function repeated only once (in app/app_model.php).

The example model they give defines an isUnique method that essentially runs an automatic check (a count() call) on the table to see if the given information exists. The example Model, View, and Controller are all given.

tagged: unique record cakephp model form information count database unique record cakephp model form information count database

Link:

The Bakery:
Checking for duplicate records (unique record)
Jan 23, 2007 @ 21:33:00

On The Bakery, there's a new expanded tutorial (from this) that talks about how to check for duplicate records in your CakePHP model.

[Here's how to] validate a form field (such as a user name field), both in add and edit form and make sure that the selected user name does not already exist in the database [via a] function repeated only once (in app/app_model.php).

The example model they give defines an isUnique method that essentially runs an automatic check (a count() call) on the table to see if the given information exists. The example Model, View, and Controller are all given.

tagged: unique record cakephp model form information count database unique record cakephp model form information count database

Link:


Trending Topics: