Since I am aware that one can call a member function on Class stored as String. But I was wondering if there is any way to do the same for static member function in PHP.
for example:
class A
{
public static function run(){
echo "OK";
}
}
"A"::"run"()
Something similar to the above example.
Please help me out. Thanks in advance.
you can call class as string like this
<?php
class A
{
public static function run()
{
echo "OK";
}
}
$stringClass = "A";
$staticMethod = "run";
$stringClass::$staticMethod();
To tackle this type of problem, which I faced while developing a user-friendly PHP framework, is to store the class name and static method name as a string in some PHP variables.
$className = "A";
$methodName = "run";
And call it like so:
$className::$methodName();
or second way
$func = "A::run";
$func(); //or
"A::run"();
And this is how one calls the static method on the class stored as a string.
This one of the ways, more can be found at https://www.designcise.com/web/tutorial/how-to-dynamically-invoke-a-class-method-in-php
Related
The idea here is to make a class that constructs with a function and an array of parameters and calls that function in a new thread.
This is my class so far:
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters[0], $this->aParmeters[1], ...);
}
}
Obviously the run function is incorrect, which brings me to my question:
Assuming the array is guaranteed to have the proper number of elements to match the function that is being called, How can I call a function in PHP with an unknown number of arguments that are stored in an array?
Edit:
Also I have no access to the contents of the given function so it cannot be edited.
Edit 2: I'm looking for something similar to scheme's curry function.
As of PHP 5.6, this is now possible. Arrays can be expanded into the argument list using the ... operator like so:
<?
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction(... $this->aParmeters);
}
}
?>
See here for more information
I think that function should accept in its case the arg array
class FunctionThread extends Thread {
public function __construct($fFunction, $aParameters){
$this->fFunction = $fFunction;
$this->aParameters = $aParameters;
}
public function run(){
$this->fFunction($this->aParmeters);
}
public function fFunction($arr){
$var0 = $arr[0];
$var1 = $arr[1];
...
do
..
}
}
I want to get static method from class and copy it to variable.
This is non-working example illustrating my question:
class foo
{
public static function bar($argument){ return 2*$argument; }
}
$class = new ReflectionClass('foo');
// here is no ReflectionMethod::getClosure() method in reality
$lambda = $class->getMethod('bar')->getClosure();
echo $lambda(3);
So my question: is this possible by any normal way? I find only one way for now. I can parse source file, get method source from it and convert it using create_function() but it's too perverse.
Just wrap it with closure.
$lamda = function($argument){return foo::bar($argument);};
Or you can try to use something like this
function staticMethodToClosure($class, $method) {
return function($argument)use($class, $method){return $class::$method($argument);};
}
An array in the format array($className, $methodName) is invokable as a static method call so this may work for you.
class foo
{
public static function bar($argument){ return 2*$argument; }
public static function getStaticFunction($arg){
return array("foo", $arg);
}
}
$a = foo::getStaticFunction("bar");
echo $a(5); // echos 10
Why won't it shuffle the array so I get a random result each time?
class greeting {
public $greet = array('hi','hello');
shuffle($greet);
}
$hi = new greeting;
echo $hi->greet[1];
Is their something wrong with my code?
If you change it so the shuffle is inside the constructor it should work fine.
class greeting {
public $greet = array('hi','hello');
function __construct(){
shuffle($this->greet);
}
}
any calculation can not be executed outside the method, inside class.
class greeting {
public $greet = array('hi','hello');
function __construct()
{
shuffle($this->greet);
}
}
$hi = new greeting;
echo $hi->greet[1];
Inside a class block you can only define constants, properties (both with fixed values) and methods. You can't put code in that block, code can only be placed inside methods (AKA functions).
I trying to learn OOP and I've made this class
class boo{
function boo(&another_class, $some_normal_variable){
$some_normal_variable = $another_class->do_something();
}
function do_stuff(){
// how can I access '$another_class' and '$some_normal_variable' here?
return $another_class->get($some_normal_variable);
}
}
and I call this somewhere inside the another_class class like
$bla = new boo($bla, $foo);
echo $bla->do_stuff();
But I don't know how to access $bla, $foo inside the do_stuff function
<?php
class Boo
{
private $bar;
public function setBar( $value )
{
$this->bar = $value;
}
public function getValue()
{
return $this->bar;
}
}
$x = new Boo();
$x->setBar( 15 );
print 'Value of bar: ' . $x->getValue() . PHP_EOL;
Please don't pass by reference in PHP 5, there is no need for it and I've read it's actually slower.
I declared the variable in the class, though you don't have to do that.
Ok, first off, use the newer style constructor __construct instead of a method with the class name.
class boo{
public function __construct($another_class, $some_normal_variable){
Second, to answer your specific question, you need to use member variables/properties:
class boo {
protected $another_class = null;
protected $some_normal_variable = null;
public function __construct($another_class, $some_normal_variable){
$this->another_class = $another_class;
$this->some_normal_variable = $some_normal_variable;
}
function do_stuff(){
return $this->another_class->get($this->some_normal_variable);
}
}
Now, note that for member variables, inside of the class we reference them by prefixing them with $this->. That's because the property is bound to this instance of the class. That's what you're looking for...
In PHP, constructors and destructors are written with special names (__construct() and __destruct(), respectively). Access instance variables using $this->. Here's a rewrite of your class to use this:
class boo{
function __construct(&another_class, $some_normal_variable){
$this->another_class = $another_class;
$this->some_normal_variable = $another_class->do_something();
}
function do_stuff(){
// how can I access '$another_class' and '$some_normal_variable' here?
return $this->another_class->get($this->some_normal_variable);
}
}
You need to capture the values in the class using $this:
$this->foo = $some_normal_variable
I know you can assign a function's return value to a variable and use it, like this:
function standardModel()
{
return "Higgs Boson";
}
$nextBigThing = standardModel();
echo $nextBigThing;
So someone please tell me why the following doesn't work? Or is it just not implemented yet? Am I missing something?
class standardModel
{
private function nextBigThing()
{
return "Higgs Boson";
}
public $nextBigThing = $this->nextBigThing();
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing; // get var, not the function directly
I know I could do this:
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing(); // Call to the function itself
But in my project's case, all of the information stored in the class are predefined public vars, except one of them, which needs to compute the value at runtime.
I want it consistent so I nor any other developer using this project has to remember that one value has to be function call rather then a var call.
But don't worry about my project, I'm mainly just wondering why the inconsistency within PHP's interpreter?
Obviously, the examples are made up to simplify things. Please don't question "why" I need to put said function in the class. I don't need a lesson on proper OOP and this is just a proof of concept. Thanks!
public $nextBigThing = $this->nextBigThing();
You can only initialize class members with constant values. I.e. you can't use functions or any sort of expression at this point. Furthermore, the class isn't even fully loaded at this point, so even if it was allowed you probably couldn't call its own functions on itself while it's still being constructed.
Do this:
class standardModel {
public $nextBigThing = null;
public function __construct() {
$this->nextBigThing = $this->nextBigThing();
}
private function nextBigThing() {
return "Higgs Boson";
}
}
You can't assign default values to properties like that unless that value is of a constant data type (such as string, int...etc). Anything that essentially processes code (such as a function, even $_SESSION values) can't be assigned as a default value to a property. What you can do though is assign the property whatever value you want inside of a constructor.
class test {
private $test_priv_prop;
public function __construct(){
$this->test_priv_prop = $this->test_method();
}
public function test_method(){
return "some value";
}
}
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel(); // corection
echo $standardModel->nextBigThing();