From procedural to object oriented

function wppg_encrypt($input, $ky)
{
    $key   = html_entity_decode($ky);
    $size  = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, 'cbc');
    $input = wppg_pkcs5_pad($input, $size);
    $td    = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', 'cbc', '');
    $iv    = "@@@@&&&&####$$$$";
    mcrypt_generic_init($td, $key, $iv);
    $data = mcrypt_generic($td, $input);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    $data = base64_encode($data);
    return $data;
}

Can we convert this procedural code to OOP? If yes, what advantage will we have?

Please let me know if I am suppose to provide some extra information. Thanks.

Have you tried creating a Class?

My knowledge of Classes is limited…

benefits:

  1. Class would be more robust
  2. all the function/method calls in a single class
  3. eliminate possibility of missing functions.
  4. able to pass initial parameters to _construct method
  5. able to use PHP7
    a. declare type_checking
    b. set types of all function parameters
    c. set function return types
    d. easier to use in other applications
1 Like

why?

Ehm. How?

All these things are also true for functions, and are not advantages specific to classes

In my opinion the advantage of OOP is encapsulation, meaning a class is responsible for a bit of data and has method to allow modifying/getting that data, without anyone having to know how the data is represented internally.

That being said, the code the OP posted won’t benefit from this since it doesn’t rely on any state. It could maybe be moved to a static function in a class, but if it’s already a function I’d just leave it.

Also, you should know that mcrypt has been deprecated in PHP 7.1 and is removed in PHP 7.2. See https://secure.php.net/manual/en/migration71.deprecated.php

3 Likes

Yesterday, I completed oops from this resource → https://laracasts.com/series/object-oriented-bootcamp-in-php

I download a very little wordpress plugin which has only two files. I was planning to convert this procedural code into OOPS.

these are only two files that this have. Can it be converted through OOPS methods?
Intention is to learn through a live project.

wc-paytm.php (18.9 KB)
function.php (5.7 KB)

It could be converted, but this is a very bad candidate, as the code as very procedural in nature. So even though you could convert it to classes it doesn’t make sense here, so won’t really get a grasp on OOP.

I would suggest you find a different example.

1 Like

Thanks for the guidance sir. Thank you so much. Do you have any such kind of small project recommendation?

I’m thinking it might be more educational to go in the opposite direction. That is, find a simple class that you know works, and then see what would be involved and what limitations there might be rewriting the class as a group of separate functions.

1 Like

I did not recognise all the functions and thought they were not PHP specific and better suited to a private class functions.

same reason as above.

The function was not taking advantage of any PHP 7 features and could be written as a self contained script with type checking.

As mentioned my knowledge of classes is limited and I do prefer classes because they seem more robust by being encapsulated if that is the correct terminology.

1 Like

Okies. Can you suggest any example? As a novice its not easy for me to find such example.

In those files I also find this one →

function wppg_ChecksumParamPattern($param)
{
    $pattern[0]     = "%,%";
    $pattern[1]     = "%#%";
    $pattern[2]     = "%\(%";
    $pattern[3]     = "%\)%";
    $pattern[4]     = "%\{%";
    $pattern[5]     = "%\}%";
    $pattern[6]     = "%<%";
    $pattern[7]     = "%>%";
    $pattern[8]     = "%`%";
    $pattern[9]     = "%!%";
    $pattern[10]    = "%\\$%";
    $pattern[11]    = "%\%%";
    $pattern[12]    = "%\^%";
    $pattern[13]    = "%=%";
    $pattern[14]    = "%\+%";
    $pattern[15]    = "%\|%";
    $pattern[16]    = "%\\\%";
    $pattern[17]    = "%:%";
    $pattern[18]    = "%'%";
    $pattern[19]    = "%\"%";
    $pattern[20]    = "%;%";
    $pattern[21]    = "%~%";
    $pattern[22]    = "%\[%";
    $pattern[23]    = "%\]%";
    $pattern[24]    = "%\*%";
    $pattern[25]    = "%&%";
    $ChecksumParamPattern = preg_replace($pattern, "", $param);
    return $ChecksumParamPattern;
}

What is this functions actually doing?

Very inefficiently removing each of the characters between % and % from the string $param and generating a warning that $pattern is not defined.

1 Like

Please explain?

It’s using preg_replace to replace single characters. Regex is significantly slower than str_replace which would be faster and the code would be considerably less confusing to read/write.

1 Like

Can you write and post the modified code?

Take a look at http://php.net/manual/en/function.str-replace.php

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.