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

Simon Holywell:
Importing and aliasing PHP functions
Oct 24, 2016 @ 16:34:29

In this recent post to his site Simon Holywell continues his look at namespacing in PHP with a look at importing and aliasing specific functions, not the entire class.

As a follow on to my short post about namespaces and functions from a year ago I thought it would be worth covering importing a specific function and aliasing functions via namespace operators too. This has been possible since PHP 5.6, but there is a nice addition in PHP 7 I’ll cover towards the end.

He starts with a refresher example of pulling in a namespace and using a method with the "use" statement. Following this he shares an update that just imports the one method via a "use function" call rather than the entire class/namespace. He again refactors this into something more usable (the original method name is quite long) with an alias. He then ends the post with the PHP 7 only trick using the braces to define grouped namespace handling (however, this doesn't allow for function level aliasing).

tagged: import alias function namespace grouping php7 tutorial

Link: https://www.simonholywell.com/post/2016/10/importing-and-aliasing-php-functions/

Exakat Blog:
6 good practices for "use" in PHP
Oct 14, 2016 @ 15:49:51

On the Exakat blog there's a new post sharing six good practices for "use" in PHP. The "use" keyword has a few different places it is used in PHP (like importing namespaced classes and passing in values to closures).

While reviewing code recently, I realized there are no guidelines for use. Every modern code do use ‘use’ (sic), as importing classes from composer or a framework is the norm. There are now quite a few variations in syntax for importing classes or traits. Although the impact on performance is low, as use is solved out, having a clean approach to ‘use’ is important to keep the code clean and readable. Let’s review what are the six good usage of use.

Each of the six tips they share come with a bit of explanation and code to back them up:

  • Do not import unused classes
  • Alway use alias
  • Place all use at first
  • Avoid using the same alias for different classes
  • Group use expression for easy reading
  • Limit the number of use expression

Some of them could be argued as to whether or not they're a "best practice" but it'd definitely interesting to see some tips for the use of this increasingly common little keyword.

tagged: bestpractice use statement keyword top6 import alias opinion

Link: https://www.exakat.io/6-good-practices-for-use/

Toptal.com:
Handling Intensive Tasks with Laravel
Apr 05, 2016 @ 17:48:36

The Toptal blog has a post for the Laravel users out there showing you how to handling intensive tasks in your applications. In this case it's creating a background job (run via cron) to import information from Excel spreadsheet formatted file into the database.

When dealing with time consuming resource intensive tasks, most PHP developers are tempted to choose the “quick hack route.” Don’t deny it! We’ve all used ini_set('max_execution_time', HUGE_INT); before, but it doesn’t have to be this way.

In today’s tutorial, I demonstrate how an application’s user experience may be improved (with minimal developer effort) by separating long-running tasks from the main request flow using Laravel. By making use of PHP’s ability to spawn separate processes that run in the background, the main script will respond faster to user action.

He starts with why PHP isn't particularly a good choice for long running requests and how making users wait for it to complete is a bad thing. He then walks you through the setup of a basic Laravel application that includes the maatwebsite/excel library for the Excel handling. He shows his configuration setup, both in the Nginx and Laravel side, to handle serving up the app. He uses Laravel migrations to set up the database and the models, routing and logic (controller) to handle the incoming Excel file for import. With this in place he then creates the command type to process the file and save the information it contains to the database. Finally, he ends the post with the cron configuration you'll need to handle the import, running it nightly at midnight.

tagged: tutorial intensive task excel import database laravel

Link: https://www.toptal.com/laravel/handling-intensive-tasks-with-laravel

SitePoint PHP Blog:
Your First Drupal 8 Migration
Mar 10, 2016 @ 18:20:06

In this tutorial on SitePoint.com author Daniel Sipos introduces you to the concepts behind the Migrate module in Drupal 8 and how it can be used to move content over from other sources.

Migrate is one of the most established modules in the Drupal ecosystem. So much so that with Drupal 8, a decision has been made to get some of its functionality ported and added to Drupal core. An important reason was that the traditional upgrade between major releases was replaced with a migration of Drupal 6 or 7 content and configuration to Drupal 8.

[...] In this article we are going to look at how migration works in Drupal 8 by migrating some content into node entities. For simplicity, the data we play with resides in tables in the same database as our Drupal installation.

He starts but outlining the "migration theory" and parts of the process to get the data in to your Drupal 8 instance: the source, the process and the destination. He uses a movie content example to show the setup needed to create the tables and define the configuration for the relationship to genres. He then shows how to make the migration configuration, defining the three parts and how to define the related migration classes. He ends the post with the drush command to execute the migration, get the current migration status and roll them back if something unexpected happens.

tagged: drupal8 migration data import source process destination

Link: http://www.sitepoint.com/your-first-drupal-8-migration/

Liip Blog:
Magento 2.0 Release
Nov 19, 2015 @ 15:40:01

On the Liip blog there's a post about the release of Magento 2, the latest and hugely reworked version of the popular eCommerce platform. The post walks you through the installation of this latest update using a pre-configured Vagrant machine.

I’ve downloaded my M2 sample from the official Magento website, there I also found an archive with sample data. For a setup I used a pre-configured Vagrant machine according to the installation guide for Magento server. This installation guide offers two options: easy and advanced. Let’s try the easy way first ;) M2 has an installation wizard, so it is supposed that even a none technically prepared user can install it successfully. And indeed, it looks easy.

He walks through some of the issues he had importing the data during the installation, ultimately, falling back to a command line call to push the large sample data into the platform.

You can find out more about this release and get the latest version over on the Magento website with plenty of information about what's been improved, added and how to get started using this latest version.

tagged: magento2 release install import data ecommerce platform

Link: https://blog.liip.ch/archive/2015/11/18/magento-2-0-release.html

Grok Interactive:
Importing Large CSV Files with PHP Part 1: Import Using One Query
Sep 23, 2015 @ 17:19:33

The Grok Interactive blog has posted a tutorial, the first part in a series, showing you how to work with large CSV files in PHP.

Importing CSV files into your application can become a problem when the file is really big, > 65,000 rows big. Each row of the file needs to be parsed, converted into an object, and then saved to a database. All of this needs to happen within a 30 second timeout window. It may sound like an impossible task, but there are actually a couple of solutions that can solve this problem. While working on a project at Grok, I was tasked with doing exactly that.

He talks about the method he tried initially for parsing the large files, splitting it up into different files and processing them as chunks. He points out that it relies on the file system, though, and this made it difficult to debug. He finally came up with a different, more simple solution: importing the files directly into MySQL via a LOAD DATA LOCAL INFILE command. He shows how to set this up in a controller and "importer" class that handles the upload and import via the importFileContents method (complete code included). He walks through what the code is doing and includes a few notes about the configuration of the database connection to specify additional options on the PDO connection to allow the local file load.

tagged: tutorial csv file import large processing chunk mysql load file query

Link: http://www.grok-interactive.com/blog/import-large-csv-into-mysql-with-php-part-1/

Robert Basic's Blog:
Importing Symfony2 security settings from a bundle
Aug 25, 2011 @ 16:58:40

In a new post to his blog Robert Basic takes a brief look at importing Symfony2 security settings from a bundle he's been creating back into the main configuration.

I started to work on/figuring out the security part in Symfony2 and one part where the docs fail so far is to explain how to import security settings from a bundle. Once I put some thinking into it, it’s pretty easy actually. Simply import the needed security file in your main config file.

His trick is to use the "imports" key in his YAML config file to define the resource to pull from in his config.xml. More information on the format of the security file can be found here in the Symfony documentation. It helps you define authentication mechanisms, authorization models and working with access control and roles.

tagged: import security setting symfoyny2 bundle configuration resource

Link:

Lorna Mitchell's Blog:
Github to Jira Bug Migration Script
Mar 09, 2011 @ 16:18:18

As part of a migration the Joind.in project made to track their bugs on a hosted Jira instance instead of the Issue Tracker on Github, Lorna Mitchell, one of the leads on the project has written up an import script she used to move current issues. The code is in her latest post.

I migrated only our open issues, and comments (and the comments ended up a bit weirdly formatted on the other end but this was the best they could do). It was nothing pretty or clever but in case it's useful to someone else.

The script connects to the github API and pulls down the information for the open issues including their titles, user and body of the issue. This is then used to make another connection for each to fetch their comments. The whole thing is dumped out to a CSV file that can be easily imported by the Jira team.

tagged: github jira import issue list bug migrate csv api

Link:

Lorna Mitchell's Blog:
3 Ways to Access a Namespaced PHP Class
Nov 29, 2010 @ 18:49:36

Lorna Mitchell has posted three different ways you can use to get access to a namespaced class in a PHP 5.3 application, all useful depending on where you are in the application and your needs.

After what felt like years of debate over the notation to use for PHP's namespaces, it seems like the feature itself has had relatively little use or attention since it was actually implemented in PHP 5.3. We're all used to working without it but using it does make code neater.

Her three options are:

  • Refer Namespace and Class Name
  • Import the Namespace
  • Alias the Namespace and/or Class

You can find out more about namespaces in PHP applications on the PHP manual.

tagged: namespace access method example import alias class

Link:

ThinkPHP.de:
Import and export data using PHPExcel
Jul 15, 2010 @ 15:21:12

On the ThinkPHP blog today there's a new post looking at their experience with PHPExcel to open, modify, save, etc Microsoft Excel files directly from PHP.

EAR's Spreadsheet_Excel_Writer combined with the project Spreadsheet_Excel_Reader on SourceForge was a good helper in the past - but only for BIFF5. BIFF8 support in spreadsheet excel writer has been a problem for a long time, and according to the authors, is still somewhat kludgy in the current version. So I needed an alternative. After a short research I stumbled upon PHPExcel which supports reading and writing of many formats in one API.

He lists some of these input and output formats and includes a simple example of how to use the tool. He creates a basic Excel file with a few different attributes (title, body, keywords) and show how to read in a document to extract the document's content.

tagged: import phpexcel export tutorial excel microsoft

Link:


Trending Topics: