1

I am having trouble wrapping my head around some object oriented programming concepts(I come from JavaScript land). I am writing a Wordpress plugin, and a very small subset of the overall plugin's job is to generate a sitemap for an n list of blog posts.

This function executes when someone activates the plugin in the main dashboard.

function activate() { $generator = new SitemapGenerator(); $generator->create_posts_sitemap(); } 

Here is a simplified version of the class just so you can see how it's structured. Understanding how the code works inside of it is not really of importance, but I am going to include it to provide a better overview of what I am working on.

class SitemapGenerator { public function create_posts_sitemap() { $header_xml = '<?xml version="1.0" encoding="UTF-8"?>'; $header_xml .= "\n"; $header_xml .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; $header_xml .= "\n\n"; $posts_xml = $header_xml; foreach($this->posts as $post) { $posts_xml .= "<url>\n"; $posts_xml .= "\t<loc>\t\n"; $posts_xml .= "\t\t" . esc_url(get_permalink($post)); $posts_xml .= "\n\t</loc>\t"; $posts_xml .= "\n</url>\n\n"; } $posts_xml .= "</urlset>"; $this->write_to_file('sitemap_posts', $posts_xml); } private function write_to_file($filename, $contents) { $file = get_home_path() . "/$filename.xml"; $open = fopen($file, "a"); ftruncate($open, 0); fputs($open, $contents); fclose($open); } } 

My question is this, are instances of objects removed from memory after they are used? As you can see, all this class does is generate a sitemap, and doesn't need to exist in memory right after its executed. I'm also wondering if I even need to use a class to perform this functionality(not required in the answer, but if you could elaborate on that as well, that would be great).

I am having trouble wrapping my head around the concept, as the PHP code sits on a server and I am more used to working with things in the browser environment.

3
  • 2
    Have you had a read upon Garbage Collection in PHP? php.net/manual/en/features.gc.php Commented Aug 12, 2017 at 14:44
  • Will give this a look. Definitely looks a little over my head how they explain it, but perhaps I can find some other tutorials online about garbage collection. Commented Aug 12, 2017 at 14:55
  • I found another article that does say that when a scope of action ends, everything in it is destroyed. So if that's true, then as soon as the write to file function finishes in the instance of the class, all of the memory taken up by the activate function will be destroyed. Is this correct? Commented Aug 12, 2017 at 15:00

1 Answer 1

1

@RamRaider is correct and has provided the appropriate reference. But, in a nutshell:

In Javascript land, one user is (basically) doing one thing on one computer.

In PHP land, a bazillion users are doing a wide variety of things on (basically) one computer.

Consequently, in situ memory cleanup is problematic. You'd be surprised how something that seemingly simple can get in the way of smooth operations. But, in reality, memory management doesn't scale well.

What PHP does is manage memory for you. To a degree, you can ignore it. There are variables that control this, but basically, every handful of times a PHP script is run, PHP fires off the "garbage collector" to sweep up the debris left behind by earlier scripts. The engine is actually working to destroy things both during execution and when execution completes, but it's not sweating the small stuff. Indeed, the solution actually provides substantially better memory management as more than just the execution-at-hand is being considered when straightening everything out.

So, the simplest answer to your question is "yes."

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet. That works. Pretty simple question, but I worry about memory leaks and the like. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.