Can I do this?
class A {
public function foo() { echo "whatever";}
}
class B {
static public $var;
static function initVar($var) { self::$var = $var; }
static public function bar() { return self::$var->foo(); }
}
class C {
public function baz() {
$a = new A();
B::initVar($a);
echo B::bar(); // should print "whatever"
}
}
if not, is there any way to a static method to access an given object instance ?
Yes, you can. This is usually how Singleton (a pattern where a object only should be instantiated once) is implemented. Though this pattern is considered bad...
http://en.wikipedia.org/wiki/Singleton_pattern
Example (though within the same class...):
class A
{
private static $inst;
public static function instance()
{
if (self::$inst === NULL)
{
self::$inst = new A();
}
return self::$inst;
}
/* constructor etc */
}
$a = A::instance();
$a->someMethod();
?>
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');
Say object of class B is attribute of class A. How can I call method of object of class A from method of object of class B? What would be nice solution without passing object link?
Thanks!
Here goes code sample:
class A{
var $b;
function __construct(){
$this->b = new B();
}
function f1(){
$this->b->f3();
}
function f2(){
echo 'hello!';
}
}
class B{
function f3(){
// call f2() method in object $obj(not new A())
}
}
$obj = new A();
$obj->f1();
You can use a static function
public static function f2{
echo 'hello!';
}
with f3 defined as
function f3(){
A::f2();
}
This may not ultimately be the solution you want, however. See more info here.
The only way you can access that instance's function is if you inject it on the B object as a dependency. You can inject it within the constructor, like this:
<?php
class A {
protected $b;
public function __construct() {
$this->b = new B($this);
}
public function f1() {
$this->b->f3();
}
public function f2() {
echo 'hello!';
}
}
class B {
protected $a;
public function __construct($a) {
$this->a = $a;
}
public function f3() {
$this->a->f2();
}
}
$obj = new A();
$obj->f1();
So say I have the following code,
$obj = new foo();
echo $obj;
class foo {
public function __construct()
{
return 'a';
}
}
How do I make $obj echo the string 'a'?
How do I make $obj refer to or equal what is returned by the object/class?
Need to return a value from a __construct(), and also a normal private function within another class. For example:
$obj2 = new foo2();
echo $obj2;
class foo2 {
public function __construct()
{
bar();
}
private bar()
{
return 'a';
}
}
Thanks!
you can use the magic __toString() method to convert your class to a representing string.
You should not return something in your constructor, __toString() is automaticly called if you try to use your instance as string (in case of echo).
from php.net:
<?php
// Declare a simple class
class TestClass
{
public $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
public function __toString()
{
return $this->foo;
}
}
$class = new TestClass('Hello');
echo $class;
?>
http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
Constructors in PHP are more like initialisation functions; their return value is not used, unlike JavaScript for instance.
If you want to change the way objects are normally echoed you need to provide the magic __toString() method:
class foo
{
private $value;
public function __construct()
{
$this->value = 'a';
}
public function __toString()
{
return $this->value;
}
}
A private method that would return the value can be used in a similar manner:
class foo2
{
private function bar()
{
return 'a';
}
public function __toString()
{
return $this->bar();
}
}
I'm trying to call a static magic function (__callStatic) from a member of its child class. Problem being, it goes to the non-static __call instead.
<?php
ini_set("display_errors", true);
class a
{
function __call($method, $params)
{
echo "instance";
}
static function __callStatic($method, $params)
{
echo "static";
}
}
class b extends a
{
function foo()
{
echo static::bar();
// === echo self::bar();
// === echo a::bar();
// === echo b::bar();
}
}
$b = new b();
echo phpversion()."<br />";
$b->foo();
?>
Output:
5.3.6
instance
How can I make it display "static"?
If you remove the magic method '__call', your code will return 'static'.
According to http://php.net/manual/en/language.oop5.overloading.php "__callStatic() is triggered when invoking inaccessible methods in a static context".
What I think is happening in your code is that,
You are calling static method from a non-static context.
The method call is in non-static context, so PHP searches for the magic method '__call'.
PHP triggers the magic method '_call' if it's exists. Or, if it's not exists it will call '_callStatic'.
Here is a possible solution:
class a
{
static function __callStatic($method, $params)
{
$methodList = array('staticMethod1', 'staticMethod2');
// check if the method name should be called statically
if (!in_array($method, $methodList)) {
return false;
}
echo "static";
return true;
}
function __call($method, $params)
{
$status = self::__callStatic($method, $params);
if ($status) {
return;
}
echo "instance";
}
}
class b extends a
{
function foo()
{
echo static::staticMethod1();
}
function foo2()
{
echo static::bar();
}
}
$b = new b();
echo phpversion()."<br />";
$b->foo();
$b->foo2();
In PHP there are the reserved words self and parent for accessing static methods from within a class and/or instantiated object. parent refers to inherited methods from the parent class.
class b extends a
{
function foo()
{
echo parent::bar();
}
}
EDIT: Uhm, that doesn't do the trickā¦ (using PHP 5.3.5)
$b = new b();
$b->foo(); // displays: instance
a::bar(); // displays: static
2nd EDIT: Ha, it works only, if you omit the __call()-method in class a.
class a
{
static function __callStatic($method, $params)
{
echo "static";
}
// function __call($method, $params)
// {
// echo "instance";
// }
}
class b extends a
{
function foo()
{
echo parent::bar();
}
}
$b = new b();
$b->foo(); // displays: static
a::bar(); // displays: static
From php manual:
[...] Static method calls are resolved at compile time.
When using an explicit class name the method is already identified completely and no
inheritance rules apply. If the call is done by self then self is translated to
the current class, that is the class the code belongs to.
Here also no inheritance rules apply [...]
..so im looking for a way to emulate the standard oop inheritance with static singleton.
Code explain better:
// Normal inheritance: my goal.
class Foo{
public function test(){
echo "Foo->test()\n";
}
}
class Bar extends Foo{
public function other_test()
{
echo "Bar->other_test()\n";
}
}
$obj = new Bar();
echo get_class($obj) . "\n";
$obj->test();
$obj->other_test();
/*
Output:
Bar
Foo->test()
Bar->other_test()
*/
// How i would love to do:
class Foo2{
public static function test2()
{
echo "Foo2::test2()\n";
}
// Singleton?
public static $_instance;
public static function get_instance()
{
if(is_null(self::$_instance))
{
self::$_instance = new self();
}
return self::$_instance;
}
}
class Bar2 extends Foo2{
public static function other_test2()
{
echo "Bar2::other_test2()\n";
}
}
$obj2 = Bar2::get_instance();
echo get_class($obj2) . "\n";
$obj2::test2();
$obj2::other_test2();
/*
Output:
Foo2
Foo2::test2()
Fatal error: Call to undefined method Foo2::other_test2()
*/
echo "\n-------\n";
// How im doing actually:
interface Foo3{
public static function get_instance();
}
class Bar3 implements Foo3{
// Singleton?
public static $_instance;
public static function get_instance()
{
if(is_null(self::$_instance))
{
self::$_instance = new self();
}
return self::$_instance;
}
public static function test3()
{
echo "Bar3::test3()\n";
}
public static function other_test3()
{
echo "Bar3::other_test3()\n";
}
}
$obj3 = Bar3::get_instance();
echo get_class($obj3) . "\n";
$obj3::test3();
$obj3::other_test3();
/*
Output:
Bar3
Foo3::test3()
Bar3::other_test3()
*/
The last 'way' force me to avoid the get_instance and static variables to be placed in the parent class, so I do not consider it as a best solution.. if for some reason my get_instance() function will change in the future, i dont want to edit all classes (inheritance! inheritance! we all want inheritance!)
So, is there a way or a best practices to solve this problem?
p.s: php5.3.2
The Singleton pattern in PHP is something like this:
class Singleton {
private static $instance = null;
// Constructor is private, so class cannot be instantiazed from outside
private function __construct() {
}
public static function getInstance() {
if (static::$instance === null) {
static::$instance = new Singleton();
}
return static::$instance;
}
public static function test() {
echo 'Singleton::test()';
}
public function __sleep() {
throw new Exception('Serialization is not alowed.');
}
public function __wakeup() {
throw new Exception('Serialization is not alowed.');
}
public function __clone() {
throw new Exception('Cloning is not alowed.');
}
}
For you is important that keyword static, then this:
class B extends Singleton {
public static function test2() {
echo 'B::test2()';
}
}
$b = B::getInstance();
B::test();
B::test2();
// Singleton::test()
// B::test()
Is this you looking for?