PHP 7 cool new features

Hello all,

Today, I thought to give you a quick run through about cool new features in PHP 7. PHP 7 has been there for sometime but it is still worth paying some closer look at what it has to offer to the world of PHP development. In this post I will be giving few examples which are exclusively to PHP 7 and onwards. Please note that at the time I write this post PHP 7.2.5 is available to download.

Scalar type declarations

Type declaration tells the type of the arguments needs to be passed when calling a function. Available scalar types are class, self, callable, array, int, string, float, boolean and iterable. (The latter five was introduced as part of PHP 7).

declare(strict_types=1);
function getTotal(int ...$ints)
{
return array_sum($ints);
}

var_dump(getTotal(45, 85, 134));

The above will give you this,

int(264)

With the strict_types enabled if you call the same function by passing mixed type parameters like this,

var_dump(getTotal("45", '8', 134.4));

This will give you a TypeError,

Fatal error: Uncaught TypeError: Argument 1 passed to getTotal() must be of the type integer, string given, Next TypeError: Argument 2 passed to getTotal() must be of the type integer, string given, Next TypeError: Argument 3 passed to getTotal() must be of the type integer, float given. 

For additional information, the declaration of strict type is per file basis and it will affect function parameter types as well as function return types. Continue reading

Share

How to call an Oracle Function in Symfony 2

Hi All,

How are you doing? Seems like my fingers do not want me to stay away from Symfony2 despite the Spring Bank Holiday. 🙂

Today, I am going to talk about how to call an Oracle Function in Symfony2. A function (aka stored function) is a collection of SQL statements which is used to perform various different activities within the Database. The good thing about the function is you can write your chunks of SQL inside the function and call it using a name to initiate. I will be focusing on how to call an Oracle Function in Symfony2 and if you would like to know more about Oracle functions please click here.

First, let’s take a look at a sample function which I have created and is saved in Oracle 12c Database.

create or replace 
FUNCTION testFunc123(old_bname IN VARCHAR2) 
   RETURN VARCHAR2 IS 
   BEGIN 
      IF old_bname = 'TEST' THEN
          RETURN 'Y';
      ELSE
        RETURN 'N';
      END IF;  
    END;

The role of the above created Oracle function is to check whether the value of ‘old_name’ is equal to ‘TEST’ or not. If it is ‘TEST’, then the function will return ‘Y’ otherwise ‘N’. So, now what we going to do is call this function inside a Symfony2 controller. This is really easy, thankfully to wonderful Doctrine2.

Continue reading

Share

Symfony 2, five quick tips

Hi all, today I am going to share with you five quick, but important tips in Symfony 2 programming.

  1. Use bundles where appropriate, such as to wrap set of reusable functionalities (business logic), reusable services and reusable views. But, don’t over use bundles as this will include unnecessary headache on performance and maintenance in long run.
  2. If you use a functionality in more than five different places, make it as a services function (Service container) . Also you might need to inject varies different services to the services class (Injecting services). In that case make sure you inject starting from the most specific to most generic service. If possible try not to inject big entities such as Container, unless you left out with no other option.
  3. Make the view less brainy. What I mean by that is, let the view (such as twig) to do less work than the Controller. In some rare cases this can be hardly achievable but in most cases you could easily make the view less brainy by doing most of the logical operations inside the Controller, Repository, Services etc. Let the view to render without it doing complex logical operations.
  4. Use Doctrine Queries Language (DQL) to query data instead of Repository class (such as $em->getRepository()) to get the maximum efficiency of your project. If you are querying from one table, using either option won’t make a big difference. But if you are querying varies tables through joins, definitely use DQL.
  5. In terms of security, try not to send parameters in the URL as it is, if possible (including non sensitive parameters). What I mean by that is, if you use URL patterns like this ‘route_to_page/{id1}/{id2}’, encrypt those ‘id1’ and ‘id2’ through a custom encrypt function created by you. Then decrypt (using a custom decrypt function created by you) those ‘id1’ and ‘id2’ in the Controller to continue with your logic.
    (Ideally through an injected services class which contains your security functionalities – How to create and inject services ? )
    See below,

Continue reading

Share

Many-to-one relationship mapping using Doctrine 2 in Symfony 2 in YAML

Hello doctrine lovers,

Well come back. In my previous post, we discussed how to map one-to-one relationships. If you have not read it but want to make yourself aware of one-to-one relationship mapping in Doctrine using YAML, make sure you read it before reading this post. So, today I am going to show you how to map many-to-one relationships in both unidirectional and bidirectional ways using Doctrine 2. As you are aware by now I will be using YAML instead of PHP annotations or XML. It is simply because YAML is very easy to understand (though you need to stick with proper indentation, which is not a big deal), will not run into unexpected problems, such as pre-compilers/caching engines tend to ignore comments, which will be used in annotations and few other things. By the way, in Symfony 2, you can use either YAML, Annotations or XML as part of the Doctrine configuration, but use one, not all. Symfony 2 evaluates all these methods equally. At the end of this post, you will learn a technique to evaluate your mapping whether it’s correct or not. So make sure you don’t miss anything in this post.

So back to the topic. Let’s have a quick look at what’s many-to-one relationship is all about. In simple terms, many-to-one is many rows of a table can be relate with only one row of another table. Take a look at the image below,

many-to-one-relationship

So basically, what this tells is, one supplier can have more than one product. For an in instance supplier ‘acer’ can have more than one products such as aspire e1 series laptops, e2 series laptops and so on.

Unidirectional – Many to One

In Unidirectional many-to-one mapping, you will only point to the target table from the parent table (same as in the one-to-one example in my previous post). I call ‘product‘ table as the owning side (parent table) and ‘supplier‘ table as the target table. The reason why I declare parent table to be the owning side is below, Continue reading

Share

One-to-one relationship mapping using Doctrine 2 in Symfony 2 in YAML

Hi doctrine 2 lovers,

As I promised in my previous post, here I came with my very next post, which is the relationship mapping using Doctrine 2. Before going any further make sure that you have read my previous post, which is about table mapping using Doctrine 2.  And I assume you have at least a little bit of knowledge about using Doctrine 2 YAML syntaxes and indentation, which is also highlighted in my previous post.  Ok, enough about my previous post, now let’s get back to the today’s topic 🙂

As you know, or will know in a moment, there are two major types of relationships between tables. Which are,

  1. Unidirectional
  2. Bidirectional

And with above two types, the relationship could be,

  1. One-To-One
  2. One-To-Many
  3. Many-To-One
  4. Many-To-Many

In this post I will be discussing on all eight combinations starting on Unidirectional/Bidirectional relationship types of one-to-one.

Continue reading

Share