I just wanna get return class properties in not instantiated class use. There is no way to instance this class? Please tell me...!
My example is below↓↓
<?php
class MyTest {
public static $test1 = 'a';
public static $test2 = 'b';
public static function getProperties() {
//how to code here...?
}
}
//plz return $test1, $test2
MyTest::getProperties();
Use self:: to access static properties:
public static function getProperties() {
return [self::$test1, self::$test2];
}
Related
I've this trait class:
trait Example
{
protected $var;
private static function printSomething()
{
print $var;
}
private static function doSomething()
{
// do something with $var
}
}
And this class:
class NormalClass
{
use Example;
public function otherFunction()
{
$this->setVar($string);
}
public function setVar($string)
{
$this->var = $string;
}
}
But i'm getting this error:
Fatal error: Using $this when not in object context.
How can i solve this issue? I can't use properties on a trait class? Or this isn't really a good practice?
Your problem is connected with differences between class's methods/properties and object's.
If you define a property as static - you should access it through your class like classname/self/parent ::$property.
If not static - then inside static property like $this->propertie.
For example:
trait Example
{
protected static $var;
protected $var2;
private static function printSomething()
{
print self::$var;
}
private function doSomething()
{
print $this->var2;
}
}
class NormalClass
{
use Example;
public function otherFunction()
{
self::printSomething();
$this->doSomething();
}
public function setVar($string, $string2)
{
self::$var = $string;
$this->var2 = $string2;
}
}
$obj = new NormalClass();
$obj -> setVar('first', 'second');
$obj -> otherFunction();
Static function printSomething can't access not static propertie $var!
You should define them both not static, or both static.
Using $this when not in object context because of this code
$this->form_validation->set_rules('username','Username','required');
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 an abstract class that has a number of static functions (which return a new instance of itself by using new static($args) which works fine), but I can't work out how to get the class name. I am trying to avoid putting
protected static $cn = __CLASS__;
but if unavoidable, then its not the end of the world
abstract class ExtendableObject {
static function getObject() {
return new static($data);
}
static function getSearcher() {
return new ExtendableObjectFinder(/* CLASS NAME CLASS */);
}
}
class ExtendableObjectFinder {
private $cn;
function __construct($className) {
$this->cn = $className;
}
function where($where) { ... }
function fetch() { ... }
}
To get the name of the class you can use get_class and pass $this.
Alternatively, there is get_called_class which you can use within static methods.
You don't need to use the class name explicitly, you can use self.
class SomeClass {
private static $instance;
public static function getInstance() {
if (self::$instance) {
// ...
}
}
}
CodePad.
abstract class Ghost {
protected static $var = 'I\'m a ghost';
final public static function __callStatic($method, $args = array()) {
echo self::$var;
}
}
class Person extends Ghost {
protected static $var = 'I\'m a person';
}
The call of Person::whatever() will print: I'm a ghost.
Why?
You're looking for something called Late Static Binding, which requires PHP 5.3+
"self" is used by Current class,
if you want get child static property, use "static" as :
final public static function __callStatic($method, $args = array()) {
echo **static**::$var;
}
If i extend a static class in PHP, and the parent class refers to "self::", will this refer to the self in the extended class?
So, for example
<?php
Class A
{
static $var
public static function guess(){self::$var = rand(); return $var}
}
Class B extends Class A
{
public static function getVar(){return self::$var}
}
If I ran
B::guess();
then B::getVar();
is the value for Var stored in A::$var or B::$var?
Thank you.
Late static binding was introduced in PHP 5.3, it allows you to control this behavior.
It's easy to test:
class ClassA {
public static function test(){ self::getVar(); }
public static function getVar(){ echo 'A'; }
}
class ClassB extends ClassA {
public static function getVar(){ echo 'B'; }
}
ClassA::test(); // prints 'A'
ClassB::test(); // also prints 'A'
... hope that helps :)
Additional information, usage of self or $this is different into extended classes
class ClassA {
public function test(){ self::getVar(); }
public function test2(){ $this->getVar(); }
public function getVar(){ echo 'A'; }
}
class ClassB extends ClassA {
public function getVar(){ echo 'B'; }
}
$classB = new ClassB();
$classB->test(); // prints 'A'
$classB->test2(); // prints 'B'