Web Hosting Talk







View Full Version : PHP Question?


dale1991
06-19-2006, 03:57 PM
Hey,
I was just wondering could use a class inside another class,
I have an mysql class and user sytem class and i dont know how to use the mysql class inside the user sytem.

If you can could you please show me a small example? :clap:

CreativeLogic
06-19-2006, 04:55 PM
You can have one of the classes extend the other.

class user extends database

maxymizer
06-19-2006, 05:11 PM
You can extend a class as CreativeLogic said, pass a class reference to other class or just instantiate a class within another class.

Examples:



Class mysql
{
public $user;

public function __construct($user_class_reference)
{
$this->user = $user_class_reference;
}
}



Class mysql
{
public $user;

public function __construct()
{
$this->user = new User;
}
}

deuce868
06-19-2006, 11:47 PM
You can do all kinds of things. For instance you might have a person class that also is made up of a phone class


class Phone {
public $personID;
public $phone;
public $type;

public function __construct($personID) {
// get the record from the db
$this->phone = $db['phone'];
$this->type = $db['type'];
}
}

class Person {
public $phoneRecord;
public $personID;

public function __construct($personID) {
$this->phoneRecord = new Phone($personID);
$this->personID = $personID;
}
}

$id = 10;
$person = new Person($id);
print $person->phoneRecord->phone;
print $person->phoneRecord->type;
print $person->personID;

Burhan
06-20-2006, 06:37 AM
You have two options here, composition (which is the object of another class inside a class technique); and inheritence which is where a class extends another.

Depending on the relationship between the two objects, you may choose to extend or have a composite class.

You cannot define a class within another class.

Composition Example:

class Foo1
{

public $bar;
function __construct()
{
$this->bar = 'hello';
}
function getBar() { return 'bar is '.$this->bar; }
function setBar($x) { $this->bar = $x; echo 'bar is now '.$x; }
}

class Foo2
{
private $fooObject;
function __construct()
{
$this->fooObject = new Foo1();
}
public function showVars()
{
echo $this->fooObject->getBar();
$this->fooObject->setBar('bye');
}
}

dale1991
06-20-2006, 11:51 AM
Oh yea thanks!
One more quesion.... do you use a template engine and do you use it on all your pages?

deuce868
06-20-2006, 11:56 AM
PHPSavant for most of my stuff.

dale1991
06-20-2006, 01:24 PM
Also is it possible to include somethink like ?myfile.php?page=1&id=1?