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

Ignace Nyamagana Butera:
Q&A: Enforcing enclosure with LeagueCsv
Sep 04, 2015 @ 16:19:44

Ignace Nyamagana Butera has a post has a post to his site showing how to use the LeagueCsv library for encapsulation in CSV output.

It is common knowledge that PHP’s fputcsv function does not allow enforcing the enclosure on every field. Using League CSV and PHP stream filter features let me show you how to do so step by step.

He walks you through the process of getting the library installed and using it (seven easy steps) to correctly contain the CSV values according to its contents:

  • Install league csv
  • Choose a sequence to enforce the presence of the enclosure character
  • Set up you CSV
  • Enforce the sequence on every CSV field
  • Create a stream filter
  • Attach the stream filter to the Writer object

Each step includes the code you'll need to make it work and a final result is shown at the end of the post. He does offer a few extra tips at the end of the post around some extra validation he added and where you can register the stream filter.

tagged: leaguecsv csv data output encapsulation stream filter

Link: http://nyamsprod.com/blog/2015/qa-enforcing-enclosure-with-leaguecsv/

thePHP.cc:
Software Development Fluxx
Feb 15, 2013 @ 15:06:57

If you've ever played the card game "Fluxx" (or are familiar with the ever changing rules behind software development) you can release to this new article from Sebastian Bergmann. He makes a link between the "Star Fluxx" card game and how OOP and encapsulation help make changing things easier.

Star Fluxx is a science-fiction-themed version of Fluxx, "a card game [that] is different from most other card games, in that the rules and the conditions for winning are altered throughout the game, via cards played by the players." (Wikipedia) When I heard the description of the game on the aforementioned episode of TableTop it reminded me of software development projects. Changing business rules and requirements (rules and the conditions for winning), probably sounds familiar to you, too.

He talks about how the game could be implemented effectively in OOP classes and interfaces - and how this structure would make it easier to make changes. A largely procedural codebase, however, would make the task much more challenging. The analogy breaks down a bit when it gets to the competitive nature of the game and how software development should be collaborative instead, but it's still effective.

tagged: fluxx software development game testing oop encapsulation

Link:

PHPMaster.com:
Practical Code Refactoring, Part 3 - Extensibility
Oct 25, 2012 @ 14:22:05

PHPMaster.com has posted the third part in their "Practical Code Refactoring" series - this time with a focus on Extensibility. (Part 1, Part 2).

Extensible code is a piece of code which follows re-usable, logical, well-known patterns, be it standard design patterns, or normal logical flow. Modular code tends to be highly extensible and monolithic code tends to be non-extensible, but monolithic code might be more efficient, so to solve this conundrum some practices allow developing in a modular way and deploying in a monolithic way so we can get the best of both worlds. The major aspects which we are to discuss with regard to extensible code are: logical extensibility (normal logical flow and design patterns), modular design, and decoupling and encapsulation.

He goes through each of the sections - logical extensibility, modular design and decoupling/encapsulation - and for each provides some questions to ask to help you whip your code into shape.

tagged: code refactor extensibility series decoupling encapsulation modular

Link:

Wes Shell's Blog:
Encapsulation in PHP
Oct 13, 2009 @ 14:05:16

Wes Shell has posted a new tutorial to his blog today looking at encapsulation in PHP development - containing parts of the script to make them easier to work with as a whole.

In order to understand the purpose of encapsulation you first need to understand the purpose of classes. [...] In order for them to be used properly as they were designed, you will need to limit how users of the class can interact with those characteristics and functionality.

He looks at the visibility modifiers (public/private/protected), interface functions and some sample code showing how to use them in a simple class to work with a Person's set of data.

tagged: tutorial encapsulation visibility

Link:

Blue Parabola Blog:
Objectively Oriented
Feb 18, 2009 @ 15:31:30

On the Blue Parabola blog, Matthew Turland takes a look at object-oriented programming and what core concepts lie at its heart.

What is object-oriented programming? The acronym OOP has become a bit of a buzzword in the current age of programming, to the point where the waters of its definition have become rather murky. [...] PHP may not be object-oriented, but from a purist perspective, neither is Java. What do I mean by "purist perspective?" Plain and simple: everything is either an object or a message being passed between objects (where message parameters are also objects).

He mentions one of the first languages to support objects (Simula) and the four fundamental concepts that would make a language truly OOP - abstraction, inheritance, encapsulation, polymorphism. Its his opinion, though, that while its good for languages to adhere to these four principles as much as they can, discussions about how well they adhere to them is usually just "spinning your wheels" and don't have much use.

tagged: object oriented definition abstraction inheritance encapsulation ploymorphism

Link:

Alex Netkachov's Blog:
Are setters evil?
Jun 17, 2008 @ 14:36:03

Alex Netkachov has posted his own response to this opinion on the Typical Programmer on getters and setters in object-oriented applications.

"Do not use getters and setters" looks like a hastily advise, but its meaning is very important and it is "do not break encapsulation", which moves us to the question what the encapsulation is.

He notes that encapsulation is, in essence, hiding parts of the code away so that the user/other coders only see a little bit of the magic that happens. He argues that getters and setters are a valid part of the encapsulation process and that designing a good, easy to use system almost requires them.

tagged: setter getter object oriented programming encapsulation

Link:

Typical Programmer Blog:
Doing it wrong: getters and setters
Jun 16, 2008 @ 16:19:17

According to this new post on the Typical Programmer blog, using getters and setters in your scripts only adds in a bit of unnecessary coupling and complexity to your scripts that you just don't need.

Today most of the popular programming languages support objects, limiting scope, modularity, passing by value, and sophisticated built-in types. There should be no reason to deliberately expose an object's data to the rest of the code because the language can enforce encapsulation and data hiding.

While not specific to PHP, the post does recommend against them because of one simple reason common to all languages that make them possible - they "break the encapsulation OOP offers". For them, they're like a cheat to get around bad coding practices and are not needed to make a successful application work.

tagged: getter setter break object oriented encapsulation scope bad

Link:


Trending Topics: