I have the class.
Class User {
private $_name;
private $_email;
public static function factory() {
return new __CLASS__;
}
public function test() {
}
}
and when i make a static method call using the syntax below.
User::factory();
it throws me following syntax error.
Parse error: syntax error, unexpected T_CLASS_C in htdocs/test/index.php on line 8
the error is being thrown because the Static factory() method is unable to create the object during the static method call.
and when i change the magic constant __CLASSS__ to the name of the current class i.e to User then it works.
what am i missing?
Try:
Class User {
private $_name;
private $_email;
public static function factory() {
$class = __CLASS__;
return new $class;
}
public function test() {
}
}
Not really sure why your example doesn't works. But what does work is:
public static function factory()
{
return new self();
}
Try this:
$class = __CLASS__;
return new $class;
Why don't you return self or $this?
Check out the singleton patterns: http://www.phpbar.de/w/Singleton and http://php.net/manual/language.oop5.patterns.php
Other solution would be
return clone $this;
Related
I have class defined like this:
class FileSystem
{
private static $_instance = NULL;
private function __construct()
{
...
}
public static function getInstance()
{
if (self::$_instance === NULL) { // <- line 83
self::$_instance = new FileSystem;
}
return self::$_instance;
}
...
}
The error I get when running it is:
Uncaught Error: Access to undeclared static property:
FileSystem::$_instance in
/var/www/html/.../FileSystem.php:83
Server on which the code is running has php 7.1.
Method that calls that class is in another class:
public function stream_close()
{
if (!is_resource($this->_stream)) {
return false;
}
FileSystem::getInstance()->fclose($this->_stream);
return true;
}
your code as posted in your question should work fine.
There is one reason of this kind of error and it's when you don't declare you static attribute so your error must be something like you forgot to save or that you have commented that declaration and so on.
Good day! I want to call the methods of my class at one string like this:
echo Core::method1()::method2()::method3();
But It isn't working!
It's my Class:
class Core
{
public static function method1()
{
return new self();
}
public static function method2()
{
return new self();
}
public static function method3()
{
}
}
What should I do?
Parse error: syntax error, unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM)
class singleton:
class Singleton
{
private static $_myself;
private function __construct(){}
public static function getInstance()
{
if(!isset(self::$_myself))
{
$obj = __CLASS__;
self::$_myself = new $obj;
}
return self::$_myself;
}
}
my class:
class MyApp extends Singleton
{
public function show()
{
echo 'show';
}
}
MyApp::getInstance()->show();
but not working, this error:
Call to undefined method Singleton::show()
somebody can help me?
Because you're returning a Singleton class (as you can see by your error), but should be returning a MyApp class. You can do this by using the late static binding method get_called_class() introduced in PHP 5.3:
public static function getInstance()
{
if(!isset(self::$_myself))
{
//__CLASS__ = Singleton | get_called_class() = MyApp
$obj = get_called_class();
self::$_myself = new $obj;
}
return self::$_myself;
}
self returns the actual class instance (Singleton in this case), so there is no method show. But you could use static instead of self (Differences) and change $_myself from private to protected so it is accessible in child classes.
class Singleton
{
protected static $_myself;
private function __construct(){}
public static function getInstance()
{
if(!isset(static::$_myself))
{
static::$_myself = new static;
}
return static::$_myself;
}
}
The problem is in
$obj = __CLASS__;
self::$_myself = new $obj;
You create a new instance of the class Singleton, not of the class MyApp, so the method is not available.
Now h2ooooooo was faster with his answer than I edited, see his answer regarding what to put instead of __CLASS__.
I have implemented a singleton pattern class, and who can be used in another classes like this :
class myClass {
private $attr;
public function __construct() {
$this->attr = Singleton::getInstance;
echo $this->attr::$sngtAttr; // Throw an error
// witch syntax use whithout pass by a temp var ?
}
}
Is $sngtAttr a static property?
If not, then just:
echo $this->attr->sngtAttr; instead of echo $this->attr::$sngtAttr;
will do it.
Otherwise since is static:
echo Singleton::$sngtAttr;
What is your question exactly ?
This is how you do a singleton :
<?php
class ASingletonClass
{
// Class unique instance. You can put it as a static class member if
// if you need to use it somewhere else than in yout getInstance
// method, and if not, you can just put it as a static variable in
// the getInstance method.
protected static $instance;
// Constructor has to be protected so child classes don't get a new
// default constructor which would automatically be public.
protected final function __construct()
{
// ...
}
public static function getInstance()
{
if( ! isset(self::$instance)) {
self::$instance = new self;
}
return self::$instance;
}
// OR :
public static function getInstance()
{
static $instance;
if( ! isset($instance)) {
$instance = new self;
}
return $instance;
// In that case you can delete the static member $instance.
}
public function __clone()
{
trigger_error('Cloning a singleton is not allowed.', E_USER_ERROR);
}
}
?>
Also don't forget the () when you call getInstance, it's a method, not a member.
as there are no enums in PHP I tried to do something like this:
class CacheMode{
public static $NO_CACHE = new CacheMode(1, "No cache");
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
The problem is, that I get a parse error if I run the script:
Parse error: syntax error, unexpected T_NEW
I "worked it aroud" with this:
class CacheMode{
public static function NO_CACHE(){
return new CacheMode(1, __("No cache",'footballStandings'));
}
public static function FILE_CACHE(){
return new CacheMode(2, __("Filecache",'footballStandings'));
}
public static function VALUES(){
return array(self::NO_CACHE(), self::FILE_CACHE());
}
private $id, $title;
public function getId(){
return $this->id;
}
public function getTitle(){
return $this->title;
}
private function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
It works, but I am not really happy with it.
Can anyone explain, why I can't do the static $xyz = new XYZ(); way or has a better solution for this problem?
Its annoying, I know. I solve it like
class Foo {
public static $var;
}
Foo::$var = new BarClass;
Its a little bit similar to javas "static code blocks" (or whatever they are called ^^)
The file is only includeable once anyway (because a "class already define" error occurs), so you can be sure, that also the code below the class is executed once.
As an optimization, you could store the object instance as a static field, so that you are not creating a new object every time the static method is called:
private static $noCache;
public static function NO_CACHE(){
if (self::$noCache == null){
self::$noCache = new CacheMode(1, __("No cache",'footballStandings'));
}
return self::$noCache;
}
But yes, it is annoying that you cannot assign a new object instance to a class field when you first define the field. :(
Quoting the manual page of static :
Like any other PHP static variable,
static properties may only be
initialized using a literal or
constant; expressions are not allowed.
So while you may initialize a static
property to an integer or array (for
instance), you may not initialize it
to another variable, to a function
return value, or to an object.
That is why you cannot do
public static $NO_CACHE = new CacheMode(1, "No cache");