...

View Full Version : php classes



slappyjaw
11-12-2010, 09:58 PM
Currently i have been working on this project for a networking script and i am almost done with the class that i am making for it. The thing is is that the thing is huge and all the functions for the site are in that class, is there a way to break it up so that i can have one class that calls it and other classes that branch off of that class that are accessible by the main class?!:eek: for example


// The main class
class main{
function hello(){
echo "hello!";
}
}

// Then the sub classes
class sub{
function welcome(){
echo "welcome";
}

class sub2{
function bye(){
echo "bye.";
}

// Now when i call the class can i make it so that i can do it like this.

$main = new main();
$main->hello();
$main->welcome();
$main->bye();
}
}

Thans for any help in advance!

slappyjaw
11-12-2010, 11:01 PM
ahhh never mind i took a looksy at how smarty does their business in their classes.

poyzn
11-13-2010, 07:12 AM
you can use pattern Singleton (http://php.net/manual/en/language.oop5.patterns.php)


class main {
public $sub;
public $sub2;
static private $instance;

private function __construct() {}

private function __clone() {}

static function getInstance() {
if(self::$instance == NULL) {
self::$instance = new self();
self::$instance->Init();
}
return self::$instance;
}

public function Init() {
$this->sub = self::getInstance();
$this->sub2 = new gfUsers();
}

public function hello(){
echo "hello!";
}
}

class sub {
function __construct() {
$this->main = main::getInstance();
}

function welcome(){
echo "welcome";
}
}

class sub2 {
function __construct() {
$this->main = main::getInstance();
}

function bye(){
echo "bye.";
}

function hello2() {
$this->mail->hello();
}
}


Now is magic:



$main = new main(); // init single class
$main->hello(); // prints 'hello'
$main->sub->welcome(); // prints 'welcome'
$main->sub2->bye(); // prints 'bye'
$main->sub2->hello2(); // prints 'hello'


Or you can use simple inheritance (http://php.net/manual/en/language.oop5.inheritance.php)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum