Skip to content

Instantly share code, notes, and snippets.

@alan-ps
alan-ps / sudo_without_pass.md
Last active October 13, 2021 19:47
Run sudo command without a password

In order to run any sudo commands without typing a password we should add next two lines into /etc/sudoers at the end of the file (use sudo for open this file).

User_Alias ADMINS = username
ADMINS ALL = NOPASSWD: ALL

where username is the user to whom permissions should be granted.

@alan-ps
alan-ps / composer_drush.md
Last active October 5, 2018 17:19
Installing drush using composer

Install Drush using Composer

1. Install composer globally

Detailed information about this step can be found here.

2. Drush installation

composer global require drush/drush:8.*
composer global update

We can state any Drush version we need. We can also roll back to a necessary release version of Drush using commands above. Now, we can use Drush:

@alan-ps
alan-ps / drupal7_first_install_update.md
Last active December 24, 2016 21:51
Drupal 7: use update functionality after first module installation.

Use code below in the .install file of your module (my_module).

<?php

/**
 * Implements hook_install().
 */
function my_module_install() {
  // Set an initial value for the schema version so we can run updates
 // after install (for first installation only).
/**
* $(document).ready() block.
*/
$(document).ready(function() {
console.log("Document loaded!");
});
/**
* Shorthand for $(document).ready().
*/

Install Drupal CS using Composer

1. Install composer globally

Detailed information about this step can be found here.

2. Install PHP Code Sniffer and Coder

composer global require squizlabs/php_codesniffer ^2.9
composer global require drupal/coder ^8.2

3. Use Drupal standards by default

Alternate Null Type Syntax

<?php

function myFunc(?MyObject $myObj) {
  echo 'Hello World!';
}

myFunc(null); // this is allowed

Overview

The keyword static is similar to self, except, that it related to a class which calls a method and do not related to a class which contains a call.

Example

<?php
  
abstract class Drink {
  private $category;

Functional inheritance

function Animal() {
  var that = {};
  that.eats = true;
  return that;
}

function Dog() {
 var that = Animal();
@alan-ps
alan-ps / php_private_method_call.md
Last active August 3, 2019 15:12
Call private methods and private properties from outside a class in PHP.
<?php

class MyClass {

  private function somePrivateMethod() {
    echo 'This method is private!';
  }

}

A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate.

Example
<?php

/**
 * Helper function to get $count of even numbers.
 */
function func($count) {