• 0

PHP OOP & MySQL


Question

Hey Guys,

So im just starting off with OOP, and im making a CMS with OOP Modules, and it installs all data from the OOP Module to a MySQL Database, but im not sure if this is right. (would it require $this->varname, or $obj = new Module(); )

<?php
// This File is only used as a template for Module Installers
// CMS Version supported - 1.2+ (1.1 is Manual Install)
//
class Module {
    public $name; // Module Name
    public $author; // Module author
    public $version; // Module Version
    public $adminOnly; // Defines if Admin only or User enabled (1 = admin only, 0 = users & admins)
    public $siteURL; // HTTP 1.0 Form (http://www.url.com)
    public $authorEmail; // Usually used in BETA Testing Modules & 

    // define methods
    public function install() {
        include("settings.php");
        connect();
        $query = mysql_query("INSERT INTO modules (name, author, version, adminOnly, siteURL, authorEmail) VALUES('$this-

>name', '$this->author', '$this->version', '$this->adminOnly', '$this->siteURL', '$authorEmail')");
    }
}

?> 

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

You need to instantiate (create) an object in order to use the class

$OBJECT = new CLASSNAME();

And then you can use methods and variables within the class

$OBJECT->METHOD();

$OBJECT->VARIABLE;

In your case

$obj = new Module();

$obj->install();

About the include and connect you're doing at the beginning of the install method, i suggest you to use the constructor (and destructor) to do all the initialization or closing code.

That would be:

class Module {
 	... 

 	public function __construct() {
 	 	 //All the stuff here is executed when the class is instantiated
 	 	 include("settings.php");
 	 	 connect();
 	 }

 	 // define methods
 	 public function install() {
 	 	 $query = mysql_query("INSERT INTO modules (name, author, version, adminOnly, siteURL, authorEmail) VALUES('$this->name', '$this->author', '$this->version', '$this->adminOnly', '$this->siteURL', '$authorEmail')");
 	 }

 	public function __destruct() {
 	 	 //All the stuff here is executed when the script is "terminated"
 	 	 disconnect();
 	 }
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.