Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/e/7/e76990b9-3372-4d88-8089-fce36e16744a/webperfection.net/sub/rady-nosu/wp-content/plugins/seo-ultimate/modules/class.su-module.php on line 1195
Real Solution for Your Problem! - Page 3

December 9, 2015 | In: Programovanie

php7 Anonymous Classes

Anonymous Classes

It is exactly what it says, but when can it be useful?

Ever found your self refactoring a pile of shit like the following code?

public function getEmail()
{
    // Now that we have the email we will save it as an object
    // on this class
    $emailObj = new \stdClass();
    $Results = new \stdClass();
    $Results->ID = $emailFromDb->id;
    $Results->HTMLBody = ...;
    $Results->TextBody = ...;
    ... more shit ...
    $emailObj->Results = $Results;

    return $emailObj;
}

// and here we go
public function getTemplateInformation($templateId)
{
    $emailObj = $this->getEmail($templateId);
    
    $data = (array)$emailObj;
    foreach ($data as $property => $value) {
        // do some black magic
    }
    
    $result = [
        "senderAddress" => "test1@test1.com",
        "subject" => '',
        "startDateTime" => '',
        "senderName" => "",
        "label" => isset($this->email->Results->Subject) ? $this->email->Results->Subject : "",
        "isModel" => 1,
    ];

    return (object)$result;
}

and then wherever you look you see casting it back to array?

$data = (array)$email;
foreach ($data as $property => $value) {
...

and then somewhere else

        $emailData = (object)[
            'emailId' => $emailId,
            'folderId' => $folderId,
            'emailWrapper' => $emailParts['container']
        ];

and some more

$result = (array)$result;
foreach ($result as $property => $value) {
...

and some more

    public function getTemplateInformation($templateId)
    {
        return (object)[
            \Service\EditorAbstract::EMAIL_TYPE => null,
            \Service\EditorAbstract::EMAIL_MASTER_ID => $this->templateMapper->getMasterForTemplate($templateId)
        ];
    }

Finding your self doing something like that? Then stop it! Seriously!

You know by now that you want to replace it with some model or real object, but you want to do it step by step and not brake anything.
Now how to do it? The answer is refactoring. I found anonymous classes super useful when replacing crap like that. As first step you can define anonymous class to confirm that everything still works as expected (if you have unit tests, if not – heaven help you!). And start replacing all the crap with your new object:


public function getTemplateInformation($templateId)
{
    $result = [
        "senderAddress" => "test1@test1.com",
        "subject" => '',
        "startDateTime" => '',
        "senderName" => "",
        "isModel" => 1,
    ];

    return new class($result) extends \Model\AbstractModel
    {
        function __construct($result)
        {
            // make the black magic here
        }
    };
}

And what you get is the nice model ready to be tested and you are ready for next steps.

more about php7:

if you need to install PHP7 check out this article

php7 null coalesce operator ??

php7 class return type

Share This:

  • Comments Off on php7 Anonymous Classes

Variance and Signature Validation – basically in simple layman’s term – class return type

then interesting thing I find about the example from


class A {}

interface SomeInterface
{
    public function test() : A;
}

class B implements SomeInterface
{
    public function test() : A // all good!
    {
        return null;
    }

    public function test2() : string
    {
        return 'wow';
    }
}

// all good, will not fail
$b = new B();

// will fail with TypeError: 
// Return value of B::test() must be an instance of A, null returned 
// $b->test();

$b->test2(); // all good

more about php7:

if you need to install PHP7 check out this article

php7 null coalesce operator ??

Share This:

  • Comments Off on php7 invariants and signature validation or class return type

A bit weird and unintuitive coalesce behaviour, so watch out when using it!

 

$coalesce = true === false ?? 'no';
var_dump($coalesce); // false - huh?

so it seems that it works only for null :/

 

$coalesce = null == false ?? 'no'; 
var_dump($coalesce); // true

so the only valid and straight forward example comes from php.net:

 

$coalesce = $_GET['something'] ?? 'default'; 
var_dump($coalesce); // default
 

$username = !isset($_GET['something']) ?? 'wtf';    
var_dump($username); // true
 

$_POST['second'] = 'chain';
$nc = $_GET['first'] ?? $_POST['second'] ?? $_REQUEST['third'] ?? 'wtf';
var_dump($nc); // chain
 

$nc = isset($_GET['something']) ?? 'wtf';
var_dump($nc); // false

if you need to install PHP7 check out this article

Share This:

  • Comments Off on php7 NULL coalesce operator ??

short summary on how to update to the latest php7, phpmyadmin4.5 and mysql5.7 on redhat

install latest php7:

rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
yum remove php56w php56w-common php56w-cli
yum install php70w php70w-common php70w-cli php70w-dba php70w-intl php70w-mbstring php70w-mcrypt php70w-mysql php70w-opcache php70w-pdo php70w-soap php70w-tidy php70w-xml php70w-xmlrpc

install latest mysql5.7:

yum localinstall https://dev.mysql.com/get/mysql57-community-release-el6-7.noarch.rpm
yum install mysql-community-server

install latest phpmyadmin4.5

yum remove phpMyAdmin
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum --enablerepo=remi,remi-test install phpMyAdmin
yum info phpMyAdmin
vi /etc/phpMyAdmin/config.inc.php

update

For some reason I had to manually update the /etc/httpd/conf/httpd.conf



SetHandler application/x-httpd-php

I did not bother on Debian and went with ZendServer9 tech preview

If you end up with missing recode:

Error: Package: php70w-recode-7.0.8-1.w6.x86_64 (webtatic)
           Requires: librecode.so.0()(64bit)

If you are subscribed to optional repos and actually can install it from yum you can simply do:

yum install recode

However, if the library can’t be found, you can download and install recode yourself from:

http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/
http://www6.atomicorp.com/channels/atomic/centos/6/x86_64/RPMS/recode-3.6-28.el6.art.i686.rpm

Then install:

 rpm -Uvh recode-3.6-28.el6.art.x86_64.rpm
 yum install recode # just to confirm
#finally you can install the phpmyadmin
 yum --enablerepo=remi,remi-test install phpmyadmin


credit goes to this guys:

https://webtatic.com/packages/php70
http://tecadmin.net/how-to-install-phpmyadmin-on-centos-using-yum/

Share This:

  • Comments Off on redhat 6/debian 8 – installing php7 + phpmyadmin 4.5 and mysql5.7

February 10, 2015 | In: Správa linuxu

useful list in console

reverse full time list, you can see latest as first

ls -ltrh

-l long list format
-r reverse
-h human readable size format
-t sort by modification time

Share This:

  • Comments Off on useful list in console

Some crazy stuff you can do with php5.5/5.6

 
        function omg(Callable...$omgs)
        {
            foreach ($omgs as $omg) {
                var_dump($omg($omg));
            }
        }

        omg('strlen'); // 6


        function omg2(array...$omgs)
        {
            foreach ($omgs as list($omg1, $omg2)) {
                var_dump($omg1($omg2));
            }
        }

        omg2(['strlen', 'two'], ['strlen', 'bar']); // 3, 3

Share This:

  • Comments Off on php5.5 and php5.6 new crazy futures

Lifecycle of doctrine 2 events

Doctrine 2 Events with Zend Framework 2 Entity Listener

// list of events for CRUD
GET
{"file":"EquipmentEntityListener.php","line":67,"data":"postLoad"}

POST
{"file":"EquipmentEntityListener.php","line":26,"data":"prePersist"}
{"file":"EquipmentEntityListener.php","line":56,"data":"preFlush"}
{"file":"EquipmentEntityListener.php","line":31,"data":"postPersist"}
{"file":"EquipmentEntityListener.php","line":56,"data":"preFlush"}

PUT
{"file":"EquipmentEntityListener.php","line":66,"data":"postLoad"}
// in my case I call the preFlush 3 times
{"file":"EquipmentEntityListener.php","line":56,"data":"preFlush"}
{"file":"EquipmentEntityListener.php","line":56,"data":"preFlush"}
{"file":"EquipmentEntityListener.php","line":56,"data":"preFlush"}
{"file":"EquipmentEntityListener.php","line":36,"data":"preUpdate"}
{"file":"EquipmentEntityListener.php","line":41,"data":"postUpdate"}

DELETE
{"file":"EquipmentEntityListener.php","line":67,"data":"postLoad"}
{"file":"EquipmentEntityListener.php","line":17,"data":"preRemove"}
{"file":"EquipmentEntityListener.php","line":22,"data":"postRemove"}

ZF2 Listener and Doctrine 2 Entity source code:
https://github.com/kukoman/playground

Share This:

  • Comments Off on Doctrine 2 Events with Zend Framework 2 Entity Listener

September 30, 2014 | In: Programovanie

ZF2 + doctrine2 + apigility


Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' with message 'Specifying the paths to your entities is required in the AnnotationDriver to retrieve all class names.' in MappingException.php on line 46

zabite 2h kym som nasiel ze treba len pridat toto:


'doctrine' => array(
'driver' => array(
'application_entities' => array(
'class' => 'Doctrine\\ORM\\Mapping\\Driver\\AnnotationDriver',
'cache' => 'array',
'paths' => array(

‘omfg’ => __DIR__,

),
),

Share This:

  • Comments Off on ZF2 + doctrine2 + apigility

how to get params out of the request

http://domain.ltd/controller/get/5?id=5
/apigility/resource[/:id]?lang=en_US


// module.config.php
// 'route' => '/[:controller[/:action[/:id]]]',

// apigility module.config.php
// key zf-rest
// 'collection_query_whitelist' => array('lang'),

var_dump(
$this->getEvent()->getQueryParams(),
$this->getEvent()->getParams(),
$this->getEvent()->getRouteParam('id),
$this->getEvent()->getRouteMatch()->getParam('id'),
// controller: (which goes like (new HttpRequest)->method(Params() param)...
$this->params()->fromRoute('id'),
$this->params()->fromQuery('id'),
$this->params()->fromPost('id'),
$this->params('id')
);

Share This:

  • Comments Off on zend framework 2 – how to get params

May 19, 2014 | In: Rôzne múdra

“the hosting”

len aby ste sa nedali nahodou natiahnut, tak vymakana sluzba “the hosting” od websupportu je uplny fake

kedze som to troska viac riesil tak posielam svoje “pocity” a co som sa dozvedel na ich podpore… je to sluzba ktora zdiela 30 concurent requestov medzi VSETKY domeny ktore tam date… takze ak pustite kampan tak jeden web vam zabije dalsich 10 co tam mate… pocet requestov sa neda zmenit, ram je na proces pre domenu takze ak pustite web, rest, advert, a nahodou 404 (pri 40mb/request) tak ste si minuli svoj limit 128mb a jedno z toho hodi fatal…

totalny uplny fake a fail pouzitelny maximalne na mrtve weby s minimalnou, resp. ziadnou navstevnostou

Share This:

  • Comments Off on “the hosting”