Skip to content

Instantly share code, notes, and snippets.

@grenoult
Last active June 23, 2016 05:22
Show Gist options
  • Save grenoult/2262b88b4dfae0de3001e8fdfd6176f5 to your computer and use it in GitHub Desktop.
Save grenoult/2262b88b4dfae0de3001e8fdfd6176f5 to your computer and use it in GitHub Desktop.
<?php
/*
Dependecy Injection (PHP):
Dependency Injection is where components are given their dependencies through their constructors, methods, or directly into fields.
Usually dependency is done through the constructor but it can also be done by a setter or a (public) property.
Source: http://fabien.potencier.org/what-is-dependency-injection.html
Example:
*/
class User
{
function __construct($storage)
{
$this->storage = $storage;
}
}
$storage = new SessionStorage('SESSION_ID');
$user = new User($storage);
/*
Interfaces (PHP):
Object interfaces allow you to create code which specifies which methods a class
must implement, without having to define how these methods are handled.
Interfaces are defined using the interface keyword, in the same way as a standard
class, but without any of the methods having their contents defined.
All methods declared in an interface must be public; this is the nature of an interface.
*/
interface IPersonService
{
public function Create($personObject);
}
class MySqlPerson implements IPersonService
{
public function Create($personObject)
{
// Create a new person in MySql database.
}
}
class MongoPerson implements IPersonService
{
public function Create($personObject)
{
// Mongo database creates a new person differenty then MySql does. But the code outside of this method doesn't care how a person will be added to the database, all it has to know is that the method Create() has 1 parameter (for the person object).
}
}
/*
Traits (PHP):
A Trait is simply a group of methods that you want include within another class.
Not to be confused with inheritance where your class depends on another one
and PHP can have only single inheritance. A Trait, like an abstract class,
cannot be instantiated on it’s own.
Example:
*/
trait Sharable {
public function share($item)
{
return 'share this item';
}
}
class Post {
use Sharable;
}
$post = new Post;
echo $post->share(''); // 'share this item'
?>
<script>
/*
Closure (JavaScript):
Closure is when a function can remember and access its lexical scope even when it's invoked outside its lexical scope.
Example:
*/
function foo() {
var a = 2;
function bar() {
console.log( a );
}
return bar;
}
var baz = foo();
baz(); // 2 -- Whoa, closure was just observed, man.
/*
IIFE (JavaScript):
"this" keyword (JavaScript):
*/
/*
Determining "this" (JavaScript - ES5 only):
There is ways to bind "this".
*/
/*
New binding: "this" is the new object.
*/
var bar = new foo();
/*
Explicit binding: using .call() or .apply().
In below example, "this" will be obj2.
*/
var bar = foo.call(obj2);
/*
Implicit binding: using a context.
Below, obj1 is the context. So if "foo", "this" will be obj1.
*/
var bar = obj1.foo();
/*
Default binding: context where the statement is.
Either the current scope, global or undefined in strict mode.
*/
var bar = foo();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment