PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class SecondClass {
$MyVal = 1;
function Compare () {
if (strcmp($x->DoSomething(), $this->MyVal) == 0) return TRUE;
}
}
If not, what is the best way to get something like this to work, I have also tried doing FirstClass::DoSomething() but then I have the problem of $this belonging to SecondClass.
class SecondClass {
$MyVal = 1;
$x;
//first example delegate class FirstClass
function SecondClass()
{
$this->x = new FirstClasss ();
}
function Compare () {
if (strcmp($this->x->DoSomething(), $this->MyVal) == 0) return TRUE;
}
}
second example - $x is parameter of Compare function
<?php
/* database class file */
class Database {
function __construct () {
//db connection
}
function Query ($sql) {
return mysql_query($sql);
}
}
$database = new Database;
/* session class file */
class Session {
private $Database;
function UseDatabaseObject ($Database) {
$this->Database = $Database;
}
function CleanUp () {
$this->Database->Query("DELETE LOW_PRIORITY FROM `session` WHERE DATE_ADD(`touched`, INTERVAL 2 DAY) < NOW()");
}
}
$session = new Session;
$session->UseDatabaseObject($database);
$session->CleanUp();
?>
So is this the best way I should be doing this kind of thing, this example is a lot closer to what I am trying to achieve, the only problem is I want to use the __construct to clean sessions etc but it is not possible this way so I have to manually call Session::CleanUp.
<?php
/* database class file */
class Database {
function Query ($sql) {
return mysql_query($sql);
}
}
$database = new Database;
/* session class file */
class Session {
private $Database;
function __construct ($Database) {
$this->Database = $Database;
}
function __destruct () {
$this->Database->Query("DELETE LOW_PRIORITY FROM `session` WHERE DATE_ADD(`touched`, INTERVAL 2 DAY) < NOW()");
}
}
$session = new Session($database);
?>
Or is there another way, because what I want to do is the second but without having to pass the object when creating an instance of Session but rather have it included in the Session class itself like so:
Put it simple, I do not want to initiate another class when 1 is already initiated because this is wasting resources, nor do I want to pass a variable by reference because this is messy when I will be passing about 5 to 10 at any 1 time, I do not want to extend the database class either because this means that I am duplicating functionality that is already there which I do not want.
I am basically trying to push a variable into the class without using globals, effectively a global without being global, if anyone knows how?
The '&' is not necessary in PHP 5 as passing objects 'by reference' is now the default behaviour, which means that creating a 'clone' requires extra effort.
olog-hai wrote:The '&' is not necessary in PHP 5 as passing objects 'by reference' is now the default behaviour, which means that creating a 'clone' requires extra effort.
Doesn't hurt, and its easier on my brain to remember that i want it as a reference.
The original solution about the singleton is the what I was looking for and to be honest I feel it is the best solution. If you're wondering, this is what I have got: