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.