Skip to content

Instantly share code, notes, and snippets.

@sycue
Last active November 8, 2023 08:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save sycue/7382545 to your computer and use it in GitHub Desktop.
Save sycue/7382545 to your computer and use it in GitHub Desktop.
PHP Procedural vs. Object-Oriented Programming (OOP)
<?php
// Procedural
function example_new() {
return array(
'vars' => array()
);
}
function example_set($example, $name, $value) {
$example['vars'][$name] = $value;
return $example;
}
function example_get($example, $name) {
$value = isset($example['vars'][$name]) ? $example['vars'][$name] : null;
return array($example, $value);
}
$example = example_new();
$example = example_set($example, 'foo', 'hello');
list($example, $value) = example_get($example, 'foo');
// OOP
class Example
{
private $vars = array();
public function set($name, $value)
{
$this->vars[$name] = $value;
}
public function get($name)
{
return isset($this->vars[$name]) ? $this->vars[$name] : null;
}
}
$example = new Example();
$example->set('foo', 'hello');
$value = $example->get('foo');
?>
@PMach17
Copy link

PMach17 commented Feb 1, 2019

Better than reading long article. Thanks

@Deepjyoti120
Copy link

hmmmmm

@jasonapril
Copy link

jasonapril commented Mar 10, 2019

Your procedural example can be improved.

function example_new() {
  return array();
}
function example_set(&$example, $name, $value) {
  $example[$name] = $value;
}
function example_get($example, $name) {
  return isset($example[$name]) ? $example[$name] : null;
}

$example = example_new();
example_set($example, 'foo', 'hello');
$value = example_get($example, 'foo');

@Aphyxia
Copy link

Aphyxia commented Mar 11, 2019

I like this last comment as it illustrates how well-written procedural code can be pretty much just as good as OO. The only advantage OO has on the visual side is that all the functions regarding something are inside the same curly braces, providing some readability improvements. The functions are also shorter I guess - example_set vs set.
The only other significant upside I can think of is scope. Rather than relying on prepending an underscore to functions meant to be private, you can actually assign scopes to functions. Though this rarely matters outside of building code for widespread use which might later have to be updated.
At least that's the way I see it.

@tbreuss
Copy link

tbreuss commented Apr 30, 2022

The procedural style could be even better or clearer using namespaces.

namespace example;

function init() { // new is a reserved keyword
  return [];
}

function set(&$example, $name, $value) {
  $example[$name] = $value;
}

function get($example, $name) {
  return isset($example[$name]) ? $example[$name] : null;
}

And then in another script (or namespace):

use example;

$example = example\init();
example\set($example, 'foo', 'hello');
$value = example\get($example, 'foo');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment