Code Monkey home page Code Monkey logo

php-like-js's Introduction

PHP like JS

In this mini library i wrote help classes with naive but helpful implementations to make php more pleasant and understandable for me. I love JS, but don't like PHP for many reasons. And one of them is a confusing and inconsistent syntax. So, i decided to make my life easier and wrote these classes to use them in my job.

Benefits of these classes for me:

I can use chaining to process data in arrays

Like this:

$array = [1, 2, 3, 4];
$newArray = (new JSArray)
  ->filter(function($item) {
    return $item % 2 === 0;
  })
  ->map(function($item) {
    return $item * 2;
  });
  ->getResult();

I use the function signature as in the documentation for the javascript

For example, if i want to iterate array using array_map, i can't get index of the array and the processed array. Now i can. And a callback function has the same signature in the rest of the class methods.

Easier to work with associative arrays

I don't understand why in php there two way to declare associative array.
First way:

$assocArray = (object) array(
  'one' => 1;
)
print_r($assocArray->one) // 1

Second way:

$assocArray = array(
  'one' => 1
)
print_r($assocArray['one']) // 1

I like the second way better because i can do this $assocArray[$var . $var2]. And often these two methods are mixed in one response from api. For example:

$result = json_decode($apiResponse);
// then result is equal to this
stdClass Object
(
  [name] => tisha
  [age] => 12
  [params] => Array
    (
      [character] => good
      [legs] => 4
      [colors] => Array
        (
          [0] => black
          [1] => white
        )
    )
)
// get access to colors
$colors = $result->params['colors'];

This class has a method that recursively converts this into an associative array.

Easier to work with UTF-8 encoded strings

PHP doesn't use utf-8 charset by default. And working with multibyte string in PHP is pain. You need use functions with mb_ prefix, set charset manually etc.

php-like-js's People

Contributors

hi-pyncho avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.