Thursday, November 8, 2018

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/



Wednesday, November 7, 2018

How can I use my own external class in CakePHP 3.0?

What are the naming conventions? Do I need to use a specific namespace?
Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php
Where do I put the file that contains the PHP class?
  1. Create a folder by author(here might be your name, as you are the author) in vendor
  2. Then create your class inside of it convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
How can I include it and use it in a controller or a view?
a) To include:
require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');
(replace MyClass by your foldername and MyClass.php by your filename.php)
b) To use it:
add use MyClass\MyClass; in your controller
For example I want to add MyClass in a controller. Steps that worked for me
  1. Creating vendor\MyClass folder
  2. Pasting MyClass.php in that folder
  3. adding namespace MyClass; at the top of MyClass.php
MyClass.php have following code for example:
namespace MyClass;


class MyClass
{
    public $prop1 = "I'm a class property!";

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

    public function getProperty()
    {
        return $this->prop1 . "<br />";
    }
}
  1. Adding use MyClass\MyClass; at the top of controller
  2. Then including it in my controller action. My action sample
       public function test()
     {
         require_once(ROOT .DS. "Vendor" . DS  . "MyClass" . DS . "MyClass.php");
    
         $obj = new MyClass;
         $obj2 = new MyClass;
    
         echo $obj->getProperty();
         echo $obj2->getProperty();
         exit;
     }

cakephp : how to use mysql now() function in cakephp for date fields?

I have this code in my cakephp project:
$this->data['MyObject']['expire_date'] = 'NOW()';
and...
$this->MyObject->save($this->data);
But this won't save data in database. After googling I found I can use now() like this. But it seems wrong, what is the correct way to use it?
PS: My MySQL field is of type 'DATE'. It works if I use like this:

It should be called like this: $db = $this->MyObject->getDataSource(); $this->data['MyObject']['expire_date'] = $db->expression('NOW()'));