Thursday, November 8, 2018

CakePHP : Implementing the Repository Pattern with CakePHP

Implementing the Repository Pattern with CakePHP


I must admit, my recent articles are becoming a bit obsessed around the repository pattern.  What can I say, I like it, it’s useful, and it’s not restrictive based on a language or a framework.

I’ve long professed how I dislike convoluted controllers.  CakePHP’s find method almost immediately causes this when used inside a controller.  More importantly, the code inside the find method is extremely unreadable.  This is almost more important than a large controller function!

This is where the repository pattern comes in.  At its most basic example (which some will consider overkill – you know who you are), I still think the repository pattern is clearer.

Here is an example using the regular find approach:
$user = $this->User->find('first', array('conditions' => array('id' => $id)));

Compared to a repository example:
$user = $this->UserRepository->GetById($id);

The code is almost identically; however, in the second example, it’s clear that if I were to “read” the code I am retrieving a user by id opposed to I’m finding the first user with the conditions of id being equal to the variable $id.

http://www.endyourif.com/implementing-the-repository-pattern-with-cakephp/

OOP : Traits

Summary: in this tutorial, you will learn how to use PHP traits to share functionality across independent classes, which are not in the same inheritance hierarchy.

Introduction to PHP traits

Code reuse is one of the most important aspects of object-oriented programming. In PHP, you use inheritance to enable code reuse in different classes that share the same inheritance hierarchy. To achieve code reuse, you move the common functionality of classes to method of the parent class. Inheritance makes the code very tightly coupled therefore makes the code hard to maintain.
To overcome this problem, as of version 5.4.0,  PHP introduced a new unit of code reuse named  trait.Traits allow you to reuse a set of methods freely in many different classes that does not need to be in the same class hierarchy.
Trait is similar to class but it is only for grouping methods in a fine-grained and consistent way. It is not allowed to instantiate a trait on its own.

http://www.zentut.com/php-tutorial/php-traits/

OOP : interface

Summary: in this tutorial, you will learn how to use PHP interface that is one of the most important building blocks in PHP object-oriented programming.

Introduction to PHP interface

An interface allows you to specify a list of methods that a class must implement. To define an interface, you use the interface keyword as follows:
An interface consists of methods that contain no implementation. In other words, all methods of the interface are abstract methods. An interface can have its own constants. See the following syntax:
All the methods in the interface must have public visibility level.

How to access stdclass object after a specific key value pair?

PHP : Dynamic Properties in PHP and StdClass

Languages like JavaScript and Python allow object instances to have dynamic properties. As it turns out, PHP does too. Looking at the official PHP documentation on objects and classes you might be lead to believe dynamic instance properties require custom __get and __set magic methods. They don't.
Simple, Built-in Dynamic Properties
Check out the following code listing:
class DynamicProperties { }
$object = new DynamicProperties;
print isset($object->foo) ? 't' : 'f'; // f

// Set Dynamic Properties foo and fooz
$object->foo = 'bar';
$object->fooz = 'baz';

// Isset and Unset work
isset($object->foo); // true
unset($object->foo);

// Iterate through Properties and Values
foreach($object as $property => $value)  { 
     print($property . ' = ' . $value . '<br />'); 
}

// Prints:
//   fooz = baz
Using the built-in dynamic instance properties is an order of magnitude faster (30x, by my profiling) than using magic __get and __set methods. Built in dynamic property accesses happen without invoking a method call back to PHP script.
So when does it make sense to use __get and __set? If you need more complex behavior, like calculated properties, you must use __get and __set. Also, as an astute comment points out, if you would prefer not to have dynamic properties on a class you can throw errors from __get and __set.

http://krisjordan.com/dynamic-properties-in-php-with-stdclass

Javascript: Understand JavaScript Closures With Ease

What is a closure?
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.
You create a closure by adding a function inside another function.

javascript : What is a Callback or Higher-order Function?

What is a Callback or Higher-order Function?

A callback function, also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (or executed) inside the otherFunction. A callback function is essentially a pattern (an established solution to a common problem), and therefore, the use of a callback function is also known as a callback pattern.
Consider this common use of a callback function in jQuery:
//Note that the item in the click method's parameter is a function, not a variable.
//The item is a callback function
$("#btn_1").click(function() {
  alert("Btn 1 Clicked");
});
As you see in the preceding example, we pass a function as a parameter to the clickmethod. And the click method will call (or execute) the callback function we passed to it. This example illustrates a typical use of callback functions in JavaScript, and one widely used in jQuery.
Ruminate on this other classic example of callback functions in basic JavaScript:
var friends = ["Mike", "Stacy", "Andy", "Rick"];

friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick
});
Again, note the way we pass an anonymous function (a function without a name) to the forEach method as a parameter.
So far we have passed anonymous functions as a parameter to other functions or methods. Lets now understand how callbacks work before we look at more concrete examples and start making our own callback functions.

https://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/