PHP memory allocation for Procedural and OO style methodologies

PHP is a server side scripting language that hooks into MySQL and other databases easily. PHP uses the Mysqli extension in the configuration file php.ini.

PHP and Java are similar in how the developer can write an object. Like Java, PHP has a similar architecture for accomplishing any Object Oriented (OO) tasks.

There are, however two main ways of writing code in PHP, procedural and OO.

With the procedural methodology, a developer can write a function that is independent of a class or object.

I have tried both and after testing them out it seems that OO takes more memory to run than procedural functions. The command memory_get_usage() can be used to get the total memory after a particular section of code.

For example you can run a function named getUsers() with or without parameters. To get the memory output use memory_get_usage() and type it after the function code. It is best to just structure it in a way that allows for less confusion while coding.

Here is an example of a function library with two functions, db() and getUser().

/* functions.php */

<?php

function db()

{

// connect to the database

}

function getUsers()

{

// Open a database connection

$db = db();

$query = “call getUsers()”;

// Prepare the query

$stmt = $db->prepare($query);

// Execute the query

$stmt->execute();

// Go through the result set

while($row = $stmt->fetch(PDO::FETCH_ASSOC))

{

extract($row);

/* So we don’t have to use $row[‘id’]

we use just $id

*/

$arr[] = array(‘id’=>$id,’name’=>$name,’age’=>$age);

}

// Return results as Json

return json_encode($arr);

}

?>

The previous code is in a file called functions.php. It is a function library of two functions, db() and getUser(). This is done to reduce the overhead that comes with an Object when using fields. In a separate file called index.php include the function library. Run that function while entering the command memory_get_usage() just after it is called. You can run a function in any php file. Just include the file where the function resides.

The code below illustrates an example.

/* index.php */

<?php

include_once “functions.php”;

echo getUsers();

memory_get_usage;

// 278093 — the memory in bytes allocated to the functions db() and getUsers()

?>

In OO, the function is encapsulated in the class, or object.

<?php

class Users

{

// database connection

private $conn;

// Class fields

public $id;

public $name;

public $age;

// The constructor

public function __construct($db)

{

$this->conn = $db;

}

// Encapsulated function

function getUsers()

{

$query = “call getUsers()”;

// Prepare the query

$stmt = $db->prepare($query);

// Execute the query

$stmt->execute();

// Go through the result set

while($row = $stmt->fetch(PDO::FETCH_ASSOC))

{

extract($row);

/* So we don’t have to use $row[‘id’]

we use just $id

*/

$arr[] = array(‘id’=>$id,’name’=>$name,’age’=>$age);

}

// Return results as Json

return json_encode($arr);

}

}

?>

Again in index.php

<?php

// Open a connection

$db = db();

// Create a users object

$users = new Users($db);

$users->getUsers();

memory_get_usage();

// 324233 — the memory in bytes allocated to the object and the encapsulated method

?>

The object has fields, like id, name, age. Memory gets assigned to each of these properties at runtime so that the object gets bloated before any other encapsulated functions are called. You might not even use those fields until a section of the code works with them, e.g. when inserting a row in the database.

So what is the best method for creating web apps with PHP? It seems that if you want to scale a small application and not get a performance hit, then the procedural is best. If you want to model real life objects in order to get a clearer understanding of the code, then oo is the way to go.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.