I'm trying to learn about OOP, and create a singleton to return a settings object within a PHP script. For some reason, the instance never seems to get set.
I'm echoing a message getting settings anytime the singleton goes through its construct (it's silent if it just returns the existing instance).
When this is called from within a script, I'm getting dozens of these getting settings messages, one for each time I call mySettings::getSettings()-- $instance doesn't seem to be created, even when I try to dump it immediately after it is created.
Can anyone point out where I'm going wrong?
Here's the code:
class mySettings {
private $settings;
private static $instance;
private function __construct(){
$configfile = "myconfig.ini";
if(!$this->settings = parse_ini_file( $configfile )){
die('CONFIG NOT FOUND/PARSED');
}
}
public static function getSettings(){
if ( !self::$instance ){
echo "getting settings-- no instance found!";
self::$instance = new mySettings();
var_dump(self::$instance); // <-- dumps nothing
}
return self::$instance;
}
public function setParam( $key, $value ){
$this->settings[$key] = $value;
}
public function getParam( $key ){
return $this->settings[$key];
}
}
Your approach to creating a singleton looks correct.
Try with an empty constructor, there might be an issue with that die() statement there.
Does: // <-- dumps nothing mean you see a "NULL" being rendered or nothing at all?
Maybe you've got some output buffering that hides the var_dump() output.
Related
I think I have more or less managed to get a grasp on OOP/Inheritance, and the basics of method chaining I think I understood as well. However I am still confused about how to actually use some of it.
I wanted to do something that I've seen when working with Magento before:
In Magento, I've seen some sort of "selector-function" used in method chaining. It's a little hard to put into words, but it was something along the lines of:
$categoryName = Mage::getModel('catalog/category')->load($categoryId)->getName();
It's the load($categoryId) part that interests me, as in, a function that selects some instance of something and allows me to run a function on that specific instance.
Now, I am writing a module that allows me to configure certain promotions on our website. Seeing as we'll have many different promotions and I want them to be easily configurable and modifiable, I wanted to do something similar.
So, if I wanted to be able to do something like this:
$prm = new Promotion();
$prm->addPromo('xmasPromo');
$prm->addPromo('nyPromo');
$prm->getPromo('xmasPromo')->setName('Promotion for Xmas!');
$prm->getPromo('nyPromo')->setName('Promotion for New Years!');
echo $prm->getPromo('xmasPromo')->getName(); // returns: Promotion for Xmas!
echo $prm->getPromo('nyPromo')->getName(); // returns: Promotion for New Years!
How would the class definition for that have to look like?
This may be much more simple or much more complicated than I anticipate. In either case, thanks a lot!
Edit:
So I did some testing around with the info deceze gave me, but I'm still confused.
Bad naming and putting 2 classes in 1 file aside, here's what I did:
class file:
class Promotion {
private $__arr = array();
public function addPromo($name) {
$this->__arr[$name] = new Promo();
}
public function getPromo($name) {
$this->__arr[$name];
}
}
class Promo {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $name;
}
}
and the run file:
require_once 'class.php';
error_reporting(E_ALL);
$prm = new Promotion();
$prm->addPromo('xmasPromo');
$prm->addPromo('nyPromo');
$prm->getPromo('xmasPromo')->setName('Promotion for Xmas!');
$prm->getPromo('nyPromo')->setName('Promotion for New Years!');
echo 'X: '.$prm->getPromo('xmasPromo')->getName(); // returns: Promotion for Xmas!
echo "\n";
echo 'N: '.$prm->getPromo('nyPromo')->getName(); // returns: Promotion for New Years!
This gives me Fatal error: Call to a member function setName() on a non-object in /var/www/test/index.php on line 11.
But why? Shouldn't getPromo() give me back the object?
Thanks again..
Thanks to the great guys here, it works now. In case anyone were to pass by here with the same or a similar question, here's the final, working code:
Classes:
class Promotion {
private $__arr = array();
public function addPromo($name) {
$this->__arr[$name] = new Promo();
}
public function getPromo($name) {
return $this->__arr[$name];
}
}
class Promo {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
Test file:
require_once 'class.php';
error_reporting(E_ALL);
$prm = new Promotion();
$prm->addPromo('xmasPromo');
$prm->addPromo('nyPromo');
$prm->getPromo('xmasPromo')->setName('Promotion for Xmas!');
$prm->getPromo('nyPromo')->setName('Promotion for New Years!');
echo 'X: '.$prm->getPromo('xmasPromo')->getName(); // returns: Promotion for Xmas!
echo "\n";
echo 'N: '.$prm->getPromo('nyPromo')->getName(); // returns: Promotion for New Years!
Method chaining is really simple, all it does is use one particular element of PHP's syntax:
When a function returns an object, you can directly continue with -> after that function.
The longhand version can be:
$bar = $foo->bar();
$baz = $bar->baz();
echo $baz;
$foo->bar() returns an object ($bar) which has a method baz(), and that method returns some value ($baz). This can be written in shorthand like so:
echo $foo->bar()->baz();
$foo->bar() still returns an object which has a method baz(), so you can directly call it without assigning it to an intermediate variable. Maybe this makes it more obvious:
echo ( $foo->bar() )->baz();
You're calling the baz() method on whatever $foo->bar() returns.
$prm->getPromo('xmasPromo')->setName('Promotion for Xmas!');
As such, in your above case, all you need to do is to return an object which has the method setName from getPromo. I would assume getPromo is supposed to return some object of, say, the Promo class. If the Promo class has a method setName, you're all set.
If you want to chain methods you just need to always return the object like this
class Chain {
public function firstChain() {
//do something
return $this;
}
public function secondChain() {
//do some stuff
return $this;
}
}
Than when you have an instance of the class you do like this:
$obj = new Chain();
$obj->fistChain()->secondChain();
Why can't I get access to my "incSessionCount" function inside my "newSession" function?
class Session {
private $_num_session = 0;
private function incSessionCount() {
$this->_num_session++;
}
public static function newSession($key, $value) {
if( !isset( $_SESSION[$key] ) ) {
$_SESSION[$key] = $value;
$this->incSessionCount();
return true;
} else {
return false;
}
}
}
I just played around, like making incSessionCount() public and so on...
And then I thought, that it must be even accessible, when it's set to private ...
It's possible, that I missed a useful article, which should have helped me, but finally I ended up asking.
So why doesn't this work?
The problem is that your newSession is static, thus you are not supposed to call instance methods from it.
I suppose you're trying to do:
Session::newSession($key, $value);
instead of
$session = new Session();
$session->newSession($key, $value);
The error is not because you're calling a private method from within a public one, but because you're using $this instead of self.
$this special variable represents the current instance object while self represents the class itself.
If you enable error display and set error reporting level to E_ALL, you will see the problem is about using $this in a wrong context.
See below theses little modifications to do what you want, and check theses pages about
class Session {
private $_num_session = 0;
private static $inst = null;
public static function instance(){
if (!static::$inst)
static::$inst = new Session();
return static::$inst;
}
private function incSessionCount() {
$this->_num_session++;
}
public static function newSession($key, $value) {
if( !isset( $_SESSION[$key] ) ) {
$_SESSION[$key] = $value;
Session::getInstance()->incSessionCount();
return true;
} else {
return false;
}
}
}
You can look for design pattern and singleton on internet, and use magic __clone() to forbid more than one instance
I only found the german version of the documentation, I don't know why : http://de.php.net/manual/de/language.oop5.patterns.php
EDIT: Check this link about design patterns : http://www.phptherightway.com/pages/Design-Patterns.html
Remember, that static methods are binded with the class. Non-static methods are binded with the instance (when you do something like $instance = new MyClass();). But when you call something on the static context, you don't have to have any instance.
It's the same, when you want to call something on instance($this), because in static context doesn't exist any instance.
The problem is the public method is static and you are trying to use a method for an instantiated object. In a static method the $this variable refers to other static methods and properties only.
The Situation
I have a table in a DB that contains job types, basically defined by a label and a price. I'm trying to do a simple SELECT * FROM jobtype but I can't seem to get it to work, although I've use the same block over and over again in the rest of the code. The main difference here, is that it is a singleton trying to execute the function.
The problem is that as soon as I uncomment the line $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error()); the page will stop loading at this particular point in the code.
The Code
Following is the code of my function getJobTypes():
require_once('Connections/vtconnection.php');
class JobTypes extends Singleton{
static private $job_types;
public static function getJobTypes(){
if (self::$job_types == null){
echo 'DEBUG: For now, $job_types is NULL.'."\n";
mysql_select_db($database_vtconnection, $vtconnection);
$query_job_types = 'SELECT * FROM jobtype';
$rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error());
while ($rs_row = mysql_fetch_assoc($rs_job_types)){
// let the job type identifier in the db be its index in our array
self::$job_types[$rs_row['id']]['label'] = $rs_row['label']; // job type label
self::$job_types[$rs_row['id']]['price'] = $rs_row['price']; // job type price
}
if (self::$job_types != null) echo 'DEBUG: $job_types has been populated.'."\n";
}
return self::$job_types;
}
}
Which I am calling like so:
$jt = JobTypes::getJobTypes();
Here is my singleton pattern:
class Singleton{
private static $instances = array();
final private function __construct(){
}
final public function __clone(){
trigger_error('You don\'t clone a singleton!', E_USER_ERROR);
}
final public static function getInstance(){
$c = get_called_class();
if(!isset(self::$instances[$c])){
self::$instances[$c] = new $c;
}
return self::$instances[$c];
}
}
I have turned the problem in my head, commented everything inside the getJobtypes() function and uncommented step by step. I found out the problem does happen with the mysql_query() line, just can't seem to work out why. Is something obviously wrong in my code?
Solved
As suggested here, I used global $vtconnection,$database_vtconnection; at the start of my static function and all went smooth. It is not an optimal solution but it pointed out the scope issue which I will now try to resolve.
I also got rid of the singleton pattern.
Well the most obvious thing is $database_vtconnection $vtconnection are defined nowhere in your getJobTypes function. If they're part of the class, you need $this (object) or self (static) references. More likely it looks like you're trying to use global variables, in which case you have to pull them into the scope of the function.
mysql_select_db can auto-connect (if $vtconnection isn't supplied) but only if it knows how - there's a previous connection (or possible INI config with db/host/user/pass).
To pull them into scope you need to add the line at the beginning of the function:
global $vtconnection,$database_vtconnection;`
... Or use the $GLOBALS superglobal array:
mysql_select_db($GLOBALS["database_vtconnection"],$GLOBALS["vtconnection"]);
For the record using globals isn't a particularly good solution.
I've noticed that the keyword static in PHP is not that static at all.
Lets say Elmo is my singleton:
class Elmo
{
private static $instance;
private function __construct()
{
echo 'Elmo says constructor\n';
}
public static function getInstance()
{
if (!isset(self::$instance))
self::$instance = new Elmo();
return self::$instance;
}
public function boo()
{
echo 'Elmo says boo!\n';
}
}
And the following file is just a regular .php script.
<?php
Elmo::getInstance()->boo();
Elmo::getInstance()->boo();
// Output:
// Elmo says constructor
// Elmo says boo!
// Elmo says boo!
?>
Every new page Elmo gets re-constructed. Why don't subsequent pages have the following output?
<?php
// Output:
// Elmo says boo!
// Elmo says boo!
?>
I hope someone can enlighten me on this, thanks!
because on every page load all memory is wiped ?
Static scoping does not mean it will stay in memory forever, it means that the variable operates outside the program call stack, and will persist during the execution of the script. It is still cleared after the program ends.
This is because every time you do a page load it runs {main} separately. This would be like running a java program two separate times and the static property not being retained. Elmo::$instance will only remain instantiated in the context of the same script. If you want it to work across page loads, you can serialize it in the session (or DB) and check this instead of $instance each time:
const SESSION = 'session';
public static function inst() {
!isset($_SESSION[self::SESSION]) and self::init();
self::$inst = $_SESSION[self::SESSION];
return self::$inst;
}
private static function init() {
$_SESSION[self::SESSION] = new self;
}
I've never developed before and I'm a bit puzzled, what is wrong with my syntax here?
private static $instance; //holder of Mongo_Wrapper
public $connected = true;
private $mongo = null; // The mongo connection affiliation
private $database = null; // The database we are working on
with this function:
public function mongo_connect($db_name) {
if (! self::connected) {
$this->mongo = new Mongo;
//TODO: error handle this whole sharade: throw new Kohana_Database_Exception('Cant connect', NULL, 503);
$this->connected = true;
}
$this->database = $this->mongo->$db_name; //set the database we are working on
return $connected;
}
I'm sorry, wmd-editor is giving me hell posting the code.
Thank you!
edit: $connected isn't static, the problem is it isn't working either with static or with $this. Also, this is a singleton class, I don't know if this is important or not.
edit: this is the rest of the code, here self and this worked properly:
public static function singleton($db_name) {
if (!isset(self::$instance)) {
$c = __CLASS__;
$this->$instance = new $c;
}
self::mongo_connect($db_name);
return self::$instance;
}
enter code here
if (! self::connected) {
is probably the cause of your error. You only use self when you are trying to access static class members (which connected is not), and you have to use the $-Sign at the beginning, otherwise you are asking for a class constant. So you either have to declare connected as static, or use $this-> to access it.
Take a look at static class members in the PHP manual!
Also you should really try to understand how OOP works, before writing code like this. PHP tells you that you cannot use $this, because you are not in a object context, which means that you never created an object instance using the new.
Maybe the PHP OOP Basics will help you.
Unfortunately, PHP lets you call methods statically which aren't actually, which may be causing the error here. But sooner or later (probably sooner) you will need to understand the OOP basics anyway, so play around with a few simple classes before trying to write code for productive use.
Also take a look at this sample implementation of the singleton pattern.
If you need further help on this issue, please show us how you are calling the connect method!
There we have your problem. Your are doing the following:
self::mongo_connect($db_name);
Which means "call mongo_connect statically on self".
What you actually need to do is:
self::$instance->mongo_connect();
Which is equivalent to "call mongo_connect on the singleton instance of self".
But please take a closer look on a basic PHP tutorial, because what you are doing there in your code is mostly wrong...
$this->$instance = new $c;
Is wrong in so many ways... Not only because you are using $this in a static context, but also because you are assigning the created instance to a class member with the name which is *contained in $instance, which seems to be empty... No clue how this can actually work...
x3ro is right. You also need the $this->connected syntax at the end:
return $this->connected;
If you're getting an error message when you use $this->connected, it's because your function isn't a method on the class, but a global function.
self should be used with static members (use $this->connected instead of self::connected).
UPDATE
private static function mongo_connect($db_name, $instance)
{
if (!$instance->connected) {
....
}
...
return $instance->connected;
}
public static function singleton($db_name) {
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
self::mongo_connect($db_name, self::$instance );
return self::$instance;
}