I have a method in a variable named class that I need to call dynamically, how can I call this:
$foo = "object"
Where object is a specific class
How do I call this in PHP?
$foo::method()
The wording of the question is confusing but from what I understand, if you want to set $foo to a specific class, lets call it Foo you can do this:
$foo = new Foo;
Here is our class Foo
class Foo {
public $aMemberVar = 'aMemberVar Member Variable';
public $aFuncName = 'aMemberFunc';
function aMemberFunc() {
print 'Inside `aMemberFunc()`';
}
}
If you want to access a class variable Foo and set it to a variable you can do this:
$myVar = 'aMemberVar';
print $foo->$myVar //prints "aMemberVar Member Variable"
Also to clarify $foo::method() implies that $foo is a static class, and static classes cannot be instantiated but they can call on its class method method() by using the scope resolution operator (::)
Hope this helps.
$$foo::method();
See PHP variable variables
Related
I have the following code:
<?php
class myclass {
public $var;
public $foo = $this->var;
}
// ...etc
When I execute this, I get the following error:
( ! ) Fatal error: Constant expression contains invalid operations in
E:\public_html\index.php on line 4
How can I call this variable not outside of class? (I mean, I don't want to define it like the following):
$myclass = new myclass();
$myclass->foo = $myclass->var;
Class variables can only be defined with constant values.
To make a dynamic assignment, you need to do it in your constructor:
class myclass {
public $var = "Hey there";
public $foo;
public function __construct()
{
$this->foo = $this->var;
}
}
Once you create an instance of this class $foo will have a value of "Hey there".
From the docs:
Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
This question already has answers here:
Overriding class constants vs properties
(2 answers)
Inheritance of static members in PHP
(3 answers)
Closed 4 years ago.
<?php
class A{
static $var;
function test(){
var_dump(self::$var);
}
}
class B extends A{
static $var = 'something';
}
$b = new B();
$b->test();
?>
why does this print out null and how do I fix this? It works if I do not set $var as static but i need it to be accesible without creating an instance.
$b->test(); prints null because it is null. When you do this:
class B extends A{
static $var = 'something';
}
you're not actually doing anything to the $var property in your A class. The property is defined with the static keyword, and per the docs, "A property declared as static cannot be accessed with an instantiated class object (though a static method can)." Thus you cannot inherit it (or subsequently set it) from your B class.
If you want the test() method to output anything meaningful, you need to set it statically, such as:
A::$var = "something";
Sorry this is going to sound like a ridiculous question but can methods (not the constructor) in classes take parameters in the function declaration?
All the examples I've seen of methods (not constructor) pass no variables but call variables already declared in the class with $this->someVariable inside the function body.
Yes, you certainly can:
class Foo {
public function sum($a, $b, $c) {
$sum = $a + $b + $c;
return $sum;
}
}
$foo = new Foo();
echo $foo->sum(1,2,3); //Displays 6
Of course, like any other function.
<?php
class Foo {
public function displayParameter($param) {
return $param;
}
}
$foo = new Foo();
echo $foo->displayParameter("Hello World"); //Displays Hello World
?>
It depends. If a class represents some object, then you will have properties that can be accessed by the functions (methods). Static methods often accept parameters. So the answer is, yes, methods can accept parameters, but it also depends on how you use the class.
can methods (not the constructor) in classes take parameters in the function declaration?
Yes.
Yes. You can pass parameters directly or by reference if you want to change their value within the called method. Let me recommend a bit of reading: http://php.net/manual/en/index.php
If effect, I have this in progress:
Class Foo {
$bar = new Bar();
protected function Spoon() {
get_class($this);
}
}
Class Bar extended Foo {
$this::Spoon(); //Should show "Bar", but instead shows "Foo"
}
I want to be able to find "Bar" from Spoon(), but I always get the parent class.
I'm a little lost here. How might I get this code to work properly?
get_class() returns 'Foo', because since the Spoon() method is inherited, it's executed in the Foo class.
Using the __CLASS__ constant instead of get_class() should work as you want it to.
you can either use the late static binding (php >= 5.3) like in this answer.
protected function Spoon() {
get_called_class($this);
}
or call the function with
$this->Spoon();
Try:
echo parent::Spoon();
That will force it to referenced in the context of the parent class (Foo). You could also use get_parent_class() inside Bar:
echo get_parent_class('Bar');
I am wondering what is the difference between $this->name and $this->$name? Also does $this have to be strictly named this or can it be anything?
$this is a reserved variable name and can not be used for anything else. It specifically points you to the object your are currently working in. You have to use $this because you do not know what variable object will be assigned to.
$this->name refers to the current class's variable name
$this->$name refers to the class variable of whatever the value of $name is. Thus
$name = "name";
echo $this->$name; // echos the value of $this->name.
$name = "test";
echo $this->$name; // echos the value of $this->test
$this is a reserved name used in PHP to point to the current instance of the class you are using it in (quoting) :
The pseudo-variable $this is available
when a method is called from within an
object context. $this is a reference
to the calling object (usually the
object to which the method belongs,
but possibly another object, if the
method is called statically from the
context of a secondary object).
When using $this->name, you are accessing the property with the name name of the current object.
When using $this->$name, $name is determined before accessing the property -- which means you'll access the property which name is contained in the $name local variable.
For instance, with this portion of code :
$name = 'abc';
echo $this->$name;
You'll actually echo the content of the abc property, as if you had written :
echo $this->abc;
When doing this, you are using variable variables (quoting) :
Class properties may also be accessed
using variable property names. The
variable property name will be
resolved within the scope from which
the call is made. For instance, if you
have an expression such as $foo->$bar,
then the local scope will be examined
for $bar and its value will be used as
the name of the property of $foo. This
is also true if $bar is an array
access.
This question just popped up after an update. I liked the question, so I thought I'd add my own example of the difference.
class Test
{
public $bar = 'bar';
public $foo = 'foo';
public function __construct()
{
$bar = 'foo';
$this->bar; // bar
$this->$bar; // foo
}
}