What is a mixin?

Adapted from Wikipedia,

… a mixin is a class that contains methods for use by other classes without having to be the parent class of those other classes. …

… Mixins encourage code reuse and can be used to avoid the inheritance ambiguity that multiple inheritance can cause, or to work around lack of support for multiple inheritance in a language. A mixin can also be viewed as an interface with implemented methods.

Big O Notation

One day, when I’m less busy, I would like to come back and write about Big O Notation (as a memory refresher). But at the meantime, here is an excellent resource:

https://www.interviewcake.com/article/java/big-o-notation-time-and-space-complexity

OOP revisited

Classes

Classes are a way of describing the blueprint of an object.

Fundamental principles of OOP

1) Abstraction
2) Polymorphism
3) Inheritance
4) Encapsulation

Inheritance

Allows a class to override or extend the functionality of a base class.

Abstraction

Allows complex logic to be abstracted out. Achieved using interfaces and abstract classes.

Abstract classes

– Cannot be instantiated.
– It defines or partially implements the methods for any class that extends it.
– An abstract method cannot have an implementation.
– Any class that inherits from an abstract class, all abstract methods must be implemented by the derived class or they must be declared abstract.
– Unlike interfaces, abstract classes may have methods with full implementation and may also have defined member fields.

Interface

– An interface is not a class.
– Interfaces join types that are unrelated.
– Can only define method names, has no implementation.
– Any class that implements an interface must implement all the methods it defines or it must be declared abstract.

Difference between abstraction and interface

Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

Encapsulation

Encapsulation hides the implementation details of a class from the object. In Encapsulation, the data is not accessed directly; it is accessed through the functions present inside the class. In simpler words, attributes of the class are kept private and public getter and setter methods are provided to manipulate these attributes. Thus, encapsulation makes the concept of data hiding possible.

Polymorphism

Polymorphism is the process of using function in different ways for different object types (method overriding).

A class can implement many interfaces and inherit only one class (even if that class is abstract)

Excellent article on PHP OOP:
http://zetcode.com/lang/php/oopi/
http://www.introprogramming.info/english-intro-csharp-book/read-online/chapter-20-object-oriented-programming-principles/#_Toc362296568 (For C# but the concepts are the same)

Syntax differences between Python and PHP

As a PHP developer for over ten years, I can’t help by compare its syntax with Python when I started learning the latter recently. I anticipate this page will be a perpetual work in progress…

Variable names

PHP – ‘$’ in front of variable names.  E.g.,

$str = 'abc';

Python – No ‘$’ in front of variable names.  E.g.,

str = 'abc'

 

Semicolons

PHP – Must have a semicolon to end each line of code. E.g.,

$str = 'abc';</pre>
<pre>$str2 = 'def';</pre>
<pre>

Python – Each line must be ended with a newline.  E.g.,

str = 'abc'
str2 = 'def'

 

‘If’ statements

PHP

if ($city == "Charlotte"):
  return 183;
elseif ($city == "Tampa"):
  return 220;
else:
  return 475;
endif;

Python

– IMPORTANT: The indent must be 4 spaces.

if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    else:
        return 475

 

Functions

PHP

function render($arg = NULL) {
}

Python

def render(arg):

Arrays in PHP = Lists in Python

PHP

$array = ['a', 2, 'c'];

Python

array = ['a', 2, 'c']

Associative arrays in PHP = Dictionaries in Python

PHP

$array = [
    "foo" => "bar",
    "bar" => ['flint', 'twine', 'gemstone'],
];

Python (Syntax is very similar to JSON)

array = {
    'foo' : 'bar',
    'bar' : ['flint', 'twine', 'gemstone']
}

For loop

PHP

foreach ($arr as $value) {
}

Python

for value in arr:

PHP’s implode = Python’s join

PHP

$s = "-";
$seq = ("a", "b", "c"); # This is sequence of strings.
print implode($s, $seq); # Will print a-b-c

Python

s = "-"
seq = ("a", "b", "c") # This is sequence of strings.
print s.join( seq ) # Will print a-b-c

& operator

Constructors

PHP

function __construct()

Python

def __init__(self)

Inheritance
PHP

class Bar extends Foo

Python

class Bar(Foo)

Parent class
PHP

parent::foo()

Python
super(bar(), self).foo()

Deleting multiple git branches

Here’s something new I learned today. I was trying to clean up some stale branches but Tower only deletes one at a time even if you selected multiple of them! Well, that’s annoying. So I started searching on the internet and this seems to work:

  1. List all the branches that have the ‘PREFIX’ prefix. This is just to preview the list before deleting it.
    $ git branch -r | awk -F/ '/\/PREFIX/{print $2}'

    Note: -r shows all the remote branches. -a shows both local and remote.

  2. If it looks good, delete away! (Scroll to see the command as it is pretty long)
    $ git branch -r | awk -F/ '/\/PREFIX/{print $2}' | xargs -I {} git push origin :{}

    Note: As of Git v1.5.0, you can also delete a remote branch using:

    $ git push origin --delete <branchname>
  3. Remove local reference to those remote branches.
    $ git remote prune origin

    or

    $ git branch -D <branch1> <branch2> <branch3> 

NPM/Bower/Composer/Homebrew – What are the differences?

For a while, I have been confused with all the different package managers that are floating around. NPM, Bower, Composer, Homebrew are some of the popular ones that I have used at work. So what are they and how do they differ from each other?

  • npm – Package manager for nodes. It comes with nodejs (What exactly is nodejs?).
  • bower – Package manager for front-end web projects. It manages components that contain HTML, CSS, JavaScript, fonts or even image files. It is also an npm package (You will need npm and nodejs to install bower and to execute it. See Getting started with Bower.)
  • composer – Dependency manager for php projects (I have used this to install/update dependencies for Drupal sites)
  • Homebrew – Package manager for Mac OS X

And here is one of the ways how they cross paths with each other:
1) Install Homebrew (I use a Mac)
2) Use Homebrew to install node (It can also be downloaded from https://nodejs.org/en/, in that case – skip Step 1)
3) Install Bower with npm

npm install -g bower

4) Install jQuery with Bower
bower install jquery

Please note that Bower is not needed to install Javascript libraries – NPM can do the same. There are some articles on the web that talk about the drawbacks of using Bower:

  • https://gofore.com/stop-using-bower/
  • https://medium.com/@nickheiner/why-my-team-uses-npm-instead-of-bower-eecfe1b9afcb#.gm5edekzx

What is Composer?

Summarized from this article:
– PEAR installs dependencies globally and Composer installs them locally, in your project structure.
– PEAR is essentially a package manager and Composer is a dependency manager.

To install Composer, run:

curl -sS https://getcomposer.org/installer | php

To configure Composer:
– Create a file named composer.json in project root.
– Add dependencies to composer.json like this:

{
  "require": {
    "php": ">5.4.2",
    "other_dependency": "~1.0"
  }
}

To install dependencies as specified in composer.json:

php composer.phar install -v

To update dependencies:

php composer.phar update

To update a single library:

composer.phar update doctrine/doctrine-fixtures-bundle

Drush not able to connect to database in MAMP

Recently I was helping a coworker to set up his drush and we ran into this problem:

Drush was not able to start (bootstrap) the Drupal [error]
database.

After hours of troubleshooting, I added these lines to his .bash_profile, as suggested by this this post:

export PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php5.5.10/bin:$PATH"

export PATH
export DRUSH_PHP="/Applications/MAMP/bin/php/php5.5.10/bin/php"
export PATH=/usr/local/bin:$PATH

And voila, it’s working!