I have a class like this:
class A {
public $var = "";
function __construct() {
$this->var = "value";
}
}
And a child class like this:
class B extends A {
function __construct() {
// Is this correct?
parent::__construct();
}
function my_function() {
// Or this?
// $options is an instantiation of A.
global $options;
echo $this->var;
}
}
The problem I was having is that when I called the my_function() method, the value of var was empty. After reading on php.net for a while I found out that when a child class has its own constructor, the parent constructor is overridden which is why my variable was empty. My question is if the way I'm calling parent::__construct() is the right solution or if I should just globalize the instantiated object that I created in my script? I've done a lot of reading in comments on PHP.net and other places and I couldn't find anything concise.
With parent::method() you call the overridden method (not only the constructor), so your solution is the right one. In your case you can omit the constructor completely and just set the value, when you declare the property.
class A {
public $var = "value";
}
Additional: Global variables are ugly in every way. Use them only, if you have really good reasons to do so and never, because its more convenient.
The following "implementation" ...
$instanceOfB = new B();
$instanceOfB->my_function();
results in "value" ... as excpected.
What has been your way of calling my_function() ?
Related
My class structure is as follows,
Class Core{
public $Variable = "Test";
Class SubClass{
// functions, etc
}
// functions etc
}
I need to access the variable $Variable from within the SubClass class, but I cannot think of a way to do it. I have tried $this->this->Variable without success.
Edit While this is incorrect syntax, this is how my class system is setup (and is achieved using includes).
Assuming you had a proper inheritance model set up, you could use parent::. But your code as-is is a flat-out syntax error. You cannot nest classes like that.
Class Core {
public $var = 'test';
}
Class SubClass Extends Core {
function foo() {
$localvar = parent::$var;
}
}
comment followup:
Perhaps something more like this?
class Core {
public $Variable = 'foo';
function __construct() {
$this->subclass = new SubClass($this->variable);
}
}
class SubClass {
public $coreVariable;
function __construct(&$var) {
$this->coreVariable = $var;
}
}
I am going to answer this because the previous comments show so much ignorance about how PHP works and the scope of variables when nesting Classes and functions which is perfectly fine to do in PHP. It happens a lot when using third party classes as includes in procedural code bases.
Class Core{
public $Variable = "Test";
Class SubClass{
// functions, etc
function new()
{
global $Variable;// this brings the variable into scope
echo $Variable;
{
}
// functions etc
}
I wish there was a sub keyword that would make the code below print value on execution. There isn't though and I wonder - is there an existing way to refer to a member of a subclass?
class Main
{
static function foo()
{
echo sub::$variable;
}
}
class Sub extends Main
{
static $variable = "value";
}
Sub::foo();
I think you are looking for static::$variable. That's called Late Static Binding and is available as of PHP 5.3.
how do i call a static method from another method inside the same class?
$this->staticMethod();
or
$this::staticMethod();
self::staticMethod();
More information about the Static keyword.
Let's assume this is your class:
class Test
{
private $baz = 1;
public function foo() { ... }
public function bar()
{
printf("baz = %d\n", $this->baz);
}
public static function staticMethod() { echo "static method\n"; }
}
From within the foo() method, let's look at the different options:
$this->staticMethod();
So that calls staticMethod() as an instance method, right? It does not. This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.
$this::staticMethod();
Since PHP 5.3 you can use $var::method() to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:
self::staticMethod();
Now, before you start thinking that the :: is the static call operator, let me give you another example:
self::bar();
This will print baz = 1, which means that $this->bar() and self::bar() do exactly the same thing; that's because :: is just a scope resolution operator. It's there to make parent::, self:: and static:: work and give you access to static variables; how a method is called depends on its signature and how the caller was called.
To see all of this in action, see this 3v4l.org output.
This is a very late response, but adds some detail on the previous answers
When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between self and the class name.
Take for instance this code:
class static_test_class {
public static function test() {
echo "Original class\n";
}
public static function run($use_self) {
if($use_self) {
self::test();
} else {
$class = get_called_class();
$class::test();
}
}
}
class extended_static_test_class extends static_test_class {
public static function test() {
echo "Extended class\n";
}
}
extended_static_test_class::run(true);
extended_static_test_class::run(false);
The output of this code is:
Original class
Extended class
This is because self refers to the class the code is in, rather than the class of the code it is being called from.
If you want to use a method defined on a class which inherits the original class, you need to use something like:
$class = get_called_class();
$class::function_name();
In the later PHP version self::staticMethod(); also will not work. It will throw the strict standard error.
In this case, we can create object of same class and call by object
here is the example
class Foo {
public function fun1() {
echo 'non-static';
}
public static function fun2() {
echo (new self)->fun1();
}
}
call a static method inside a class
className::staticFunctionName
example
ClassName::staticMethod();
I have a variable on the global scope that is named ${SYSTEM}, where SYSTEM is a defined constant. I've got a lot of classes with functions that need to have access to this variable and I'm finding it annoying declaring global ${SYSTEM}; every single time.
I tried declaring a class variable: public ${SYSTEM} = $GLOBALS[SYSTEM]; but this results in a syntax error which is weird because I have another class that declares class variables in this manner and seems to work fine. The only thing I can think of is that the constant isn't being recognised.
I have managed to pull this off with a constructor but I'm looking for a simpler solution before resorting to that.
EDIT
The global ${SYSTEM} variable is an array with a lot of other child arrays in it. Unfortunately there doesn't seem to be a way to get around using a constructor...
Ok, hopefully I've got the gist of what you're trying to achieve
<?php
// the global array you want to access
$GLOBALS['uname'] = array('kernel-name' => 'Linux', 'kernel-release' => '2.6.27-11-generic', 'machine' => 'i686');
// the defined constant used to reference the global var
define(_SYSTEM_, 'uname');
class Foo {
// a method where you'd liked to access the global var
public function bar() {
print_r($this->{_SYSTEM_});
}
// the magic happens here using php5 overloading
public function __get($d) {
return $GLOBALS[$d];
}
}
$foo = new Foo;
$foo->bar();
?>
This is how I access things globally without global.
class exampleGetInstance
{
private static $instance;
public $value1;
public $value2;
private function initialize()
{
$this->value1 = 'test value';
$this->value2 = 'test value2';
}
public function getInstance()
{
if (!isset(self::$instance))
{
$class = __CLASS__;
self::$instance = new $class();
self::$instance->initialize();
}
return self::$instance;
}
}
$myInstance = exampleGetInstance::getInstance();
echo $myInstance->value1;
$myInstance is now a reference to the instance of exampleGetInstance class.
Fixed formatting
You could use a constructor like this:
class Myclass {
public $classvar;
function Myclass() {
$this->classvar = $GLOBALS[SYSTEM];
}
}
EDIT: Thanks for pointing out the typo, Peter!
This works for array too. If assignment is not desired, taking the reference also works:
$this->classvar =& $GLOBALS[SYSTEM];
EDIT2: The following code was used to test this method and it worked on my system:
<?php
define('MYCONST', 'varname');
$varname = array("This is varname", "and array?");
class Myclass {
public $classvar;
function Myclass() {
$this->classvar =& $GLOBALS[MYCONST];
}
function printvar() {
echo $this->classvar[0];
echo $this->classvar[1];
}
};
$myobj = new Myclass;
$myobj->printvar();
?>
The direct specification of member variables can not contain any references to other variables (class {public $membervar = $outsidevar;} is invalid as well). Use a constructor instead.
However, as you are dealing with a constant, why don't you use php's constant or class constant facilities?
You're trying to do something really out-of-the-ordinary here, so you can expect it to be awkward. Working with globals is never pleasant, especially not with your dynamic name selection using SYSTEM constant. Personally I'd recommend you use $GLOBALS[SYSTEM] everywhere instead, or ...
$sys = $GLOBALS[SYSTEM];
... if you're going to use it alot.
You could also try the singleton pattern, although to some degree it is frowned upon in OOP circles, it is commonly referred to as the global variable of classes.
<?php
class Singleton {
// object instance
private static $instance;
// The protected construct prevents instantiating the class externally. The construct can be
// empty, or it can contain additional instructions...
protected function __construct() {
...
}
// The clone and wakeup methods prevents external instantiation of copies of the Singleton class,
// thus eliminating the possibility of duplicate objects. The methods can be empty, or
// can contain additional code (most probably generating error messages in response
// to attempts to call).
public function __clone() {
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
public function __wakeup() {
trigger_error('Deserializing is not allowed.', E_USER_ERROR);
}
//This method must be static, and must return an instance of the object if the object
//does not already exist.
public static function getInstance() {
if (!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}
//One or more public methods that grant access to the Singleton object, and its private
//methods and properties via accessor methods.
public function GetSystemVar() {
...
}
}
//usage
Singleton::getInstance()->GetSystemVar();
?>
This example is slightly modified from wikipedia, but you can get the idea. Try googling the singleton pattern for more information
I'd say the first two things that stand out to me are:
You don't need the brackets around the variable name, you can simply do public $system or public $SYSTEM.
While PHP may not always require it it is standard practice to encapsulate non-numeric array indexes in single or double quotes in case the string you're using becomes a constant at some point.
This should be what you're looking for
class SomeClass {
public $system = $GLOBALS['system'];
}
You can also use class constants which would instead be
class SomeClass {
const SYSTEM = $GLOBALS['system'];
}
This can be referenced within the class with 'self::SYSTEM' and externally with 'SomeClass::SYSTEM'.
Suppose I have the following code:
class siteMS
{
...
function __CONSTRUCT()
{
require 'config.php';
$this->config = new siteMSConfig;
...
}
...
}
From inside the siteMSConfig class can I determine weather or not it is being called from inside the siteMS class?
Yes, but there's no "pretty" way to do it - you'll end up looking through a backtrace or something similar.
It would be better to pass an (optional?) parameter to the siteMSConfig constructor like this:
class siteMSConfig
{
public function __construct($inSiteMS = false)
{
}
}
or alternatively, subclass siteMSConfig:
class siteMSsiteMSConfig extends siteMSConfig
{
public function __construct()
{
// Possibly call parent::__construct();
}
}
Technically yes, you could use debug_backtrace to figure out who your caller was.
Writing a class which alters its behaviour based purely on where it called from is asking for a world of pain later on though. Why not parameterise the different behaviour, or make a subclass?
I guess you have to pass it with variable, from what place you called it
$this->config = new siteMSConfig ('siteMS');