A Fun Control Structure for PHPsteemCreated with Sketch.

in #programming5 years ago

I thought I would continue my rant about object oriented programming.

I personally believe that there are many different solutions to the problems one faces in life and that it is a mistake to get caught up thinking that one path is the only possible solution.

I see OOP as one of many different valid approaches to program design. Unfortunately, some advocates of OOP and Agile fall into a mindset that their way of programming is the only way to program. Java uses a class structure; therefore, every program must be shoved into a class structure.

The dictatorial mindset of the Technorati turns me off.

An object is really nothing more than a way of organizing data and procedures. The "class structure" used in C++ and Java is linguistic device designed to help guide people in writing code.

One can accomplish anything at an OOP program does with a collection of procedures and a control structure.

PHP allows variable functions; So, last night I started thinking about rewriting the Vagabond Spirit game using the following control structure:

// This is my "fun" example.
// I put all of my functions in an array.

$funArr = [null,'funOne','funTwo','funThree'];

// Here I define the functions.

function funOne($a, $b) {
  echo 'Fun One: '.$a.' plus '.$b.' is '.($a + $b);
}

function funTwo($a, $b) {
  echo 'Fun Two: '.$a.' minus '.$b.' is '.($a - $b);
}

function funThree($a, $b) {
  echo 'Fun Three: '.$a.' times '.$b.' is '.($a * $b);
}

// In this example, I test each function:

$funArr[1](8,4);
$funArr[2](8,4);
$funArr[3](8,4);

/*****
 * The output is:
 * Fun One: 8 plus 4 is 12
 * Fun Two: 8 minus 4 is 4
 * Fun Three: 8 times 4 is 32
 * Yes, I added line breaks.
*****/

Instead of using long case statements. The control structure for the program is now essentially two lines of code:

// $funID is the function I want to execute:
// $funParameters is an array holding the function's parameters.

if (exists($funArr[$funID])) {
  $funArr[$funID]($funParameters);
}

I probably should mention one more thing: I am going to put my functions in a SQLite3 database and generate my PHP code from the database.

Putting the code in a database will give me greater version control and will aide in publishing the code.

The project could show the power of using a database to hold the code instead of an IDE.

I don't actually hate OOP. For years I was an advocate of the approach. I dislike the dictatorial mindset of the technorati. There is more than one way to think. That it is the whole point of freedom. Different people try different things and society gradually improves.

Coin Marketplace

STEEM 0.36
TRX 0.12
JST 0.039
BTC 70112.96
ETH 3549.99
USDT 1.00
SBD 4.71