Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Forked from sycue/ProceduralVsOop.php
Created June 21, 2019 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanbekabe/1f2b3775e4eb66adb488bcb71d88da26 to your computer and use it in GitHub Desktop.
Save ryanbekabe/1f2b3775e4eb66adb488bcb71d88da26 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');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment