why [ ] notation is used to input element to static array [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
in the original example $methods is a static array.It is used to assign outsider functions as methods to a class.But to input new element to this array writer used the following expression
static protected $methods = array();
i removed the static keyword from the variable declaration and tried to execute the code.
protected $methods = array();
the execution gave the following error:
Fatal error: Access to undeclared static property: Dynamic::$methods
in C:\xampp\htdocs\practice\json.php on line 6
what is this [ ] notation is used for and how it is related to static keyword??
original full code:
class Dynamic {
static protected $methods = array();
public static function registerMethod($method) {
self::$methods[] = $method;
}
private function __call($method, $args) {
if (in_array($method, self::$methods)) {
return call_user_func_array($method, $args);
}
}
}
function test() {
print "Hello World" . PHP_EOL;
}
Dynamic::registerMethod('test');
$d = new Dynamic();
$d->test();

what is this [ ] notation is used for and how it is related to static keyword??
The use of static and [] are unrelated.
The code $array[] = $i is a shorthand for pushing the element $i onto the end of the array $array. It could also be written array_push($array, $i).

Related

How to share variables in php functions [duplicate]

This question already has answers here:
Sharing var from one function to the other function in PHP Class
(2 answers)
Closed 4 years ago.
Let's say I have two different functions, and one of them has a defined variable. In the second function, I don't wanna write the same variable again, can I simply use the variable from the first function in the second one WITHOUT redefining it in the second function?
Someting like:
class example{
public function a($foo){
$foo2 = $foo + 1
return $foo2;
}
public function b($foo2){
echo "result: " . $foo2;
}
}
It's simple, you can use a property $foo2 and access it from both methods:
class example{
private $foo2;
public function a($foo){
$this->foo2 = $foo + 1
return $this->foo2;
}
public function b(){
echo "result: " . $this->foo2;
}
}
$obj = new example();
$obj->a(5);
$obj->b(); // result: 6

In php class variable one equal second variable? [duplicate]

This question already has answers here:
Function literal in PHP class
(3 answers)
Closed 6 years ago.
This is my code:
class Config {
static public $site = 'http://localhost/site/';
static public $style = $site . 'css/style.css';
// ...
}
This is not work for me. I get white screen.
class Config {
static public $site = 'http://localhost/site/';
static public $style = 'http://localhost/site/css/style.css';
// ...
}
This is work. I get design and code. Work very well. My question is Why?
This is not possible:
static public $style = $site . 'css/style.css';
Expressions cannot be used to initialize class values. Only constant values are permitted. This is something you'd have to do in the constructor, but since they're static values, there's no guarantee that the object will have been initialized BEFORE you try to access those statics.
class foo {
public $foo = 1; // ok
public $bar = 1+1; // ok only in PHP 5.6+
public $baz = $this . $that; // not valid in any version of PHP
The only time you could use expressions (in php 5.6+) is if the resulting value can be calculated at compile time. so 1+1 is ok, because that can be handled at compile time. but $this . $that can only be determined at runtime, and is therefore illegal.

Returning arrays and accessing values [duplicate]

This question already exists:
Access array element from function call in php [duplicate]
Closed 8 years ago.
I'm fairly new to PHP and I am trying to return an array from a class function and then access its values like so:
<?php
class Foo
{
private $access;
public function setAccess($access)
{
$this->access = $access;
}
public function getAccess()
{
return $this->access;
}
}
$var = new Foo();
$var->setAccess(array(1,2,3,4));
$var2 = $var->getAccess()[2];
echo $var2;
?>
When I try to run a page with this code, I get the following:
Parse error: syntax error, unexpected '[' in arraytest.php on line 22
How can I access the values for my private arrays in my class?
This:
$var2 = $var->getAccess()[2];
can't be done in PHP before version 5.4 (this feature is called array dereferencing). At the moment you have to do something like this:
$var2 = $var->getAccess();
$var2 = $var2[2];
you can't call an array value directly like that, you'd have to write this:
$access = $var->getAccess();
$var2 = $access[2];
alternatively, you could add a function like this to your class.
public function getAccessValue($key) {
return $this->access[$key];
}

Calling anonymous functions defined as object variables in php [duplicate]

This question already has answers here:
Calling closure assigned to object property directly
(12 answers)
Closed 9 years ago.
I have php code like:
class Foo {
public $anonFunction;
public function __construct() {
$this->anonFunction = function() {
echo "called";
}
}
}
$foo = new Foo();
//First method
$bar = $foo->anonFunction();
$bar();
//Second method
call_user_func($foo->anonFunction);
//Third method that doesn't work
$foo->anonFunction();
Is there a way in php that I can use the third method to call anonymous functions defined as class properties?
thanks
Not directly. $foo->anonFunction(); does not work because PHP will try to call the method on that object directly. It will not check if there is a property of the name storing a callable. You can intercept the method call though.
Add this to the class definition
public function __call($method, $args) {
if(isset($this->$method) && is_callable($this->$method)) {
return call_user_func_array(
$this->$method,
$args
);
}
}
This technique is also explained in
JavaScript-style object literals

PHP: Getting a value of a static variable of an unknown subclass [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
From the string name of a class, can I get a static variable?
Somewhere in a parent class, I need to find the value of a static variable of one of the possible child classes, determined by the current instance.
I wrote:
$class = get_class($this);
$value = isset($class::$foo['bar']) ? $class::$foo['bar'] : 5;
In this example, the subclass whose name is in $class has a public static $foo.
I know using $class::$foo['bar'] is not a very beautiful piece of code, but it gets the job done on PHP 5.3.4.
In PHP 5.2.6 though, I am getting a syntax error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ')'
Is there an alternative way that would work on PHP 5.2.4+ that would get the same thing done?
EDIT: Reflection is better.
You can try the get_class_vars method. No access to PHP 5.2.6, but this works in 5.2.11...
class Test {
public static $foo;
function __construct() {
echo("...Constructing...<br/>");
Test::$foo = array();
Test::$foo['bar'] = 42;
}
function __toString() {
return "Test";
}
}
$className = 'Test';
$class = new $className();
$vars = get_class_vars($className);
echo($vars['foo']['bar'] . "<br/>");
Output:
...Constructing...
42
The reason that this does not work in PHP 5.2, is because before PHP 5.3 you are not allowed to use variables in the classname. So, if possible use eval for this.
eval('$result = ' . $c . '::$foo[\'bar\'];');
echo $result;
Otherwise, you're forced to use a function in the child class to receive the value. For example:
class MyParent {
public function __construct() {
$var = $this->_getVariable();
echo $var['bar'];
}
}
class MyChild extends MyParent {
static $var = array('bar' => 'foo');
protected function _getVariable() {
return self::$var;
}
}
new MyChild();
class Bar1 {
static $var = array('index' => 'value');
}
class Bar2 extends Bar1 {
}
class Foo extends Bar2 {
static $var = array('index' => 'value in Foo');
public function __construct() {
echo parent::$var['index'];
}
}
$foo = new Foo();
will output 'value', but not 'value in Foo'
Hope, that's what you are looking for.
You can get class static/call static method in the class you are working in using self key word or for parent class using parent. You can get that error on php 5.2.6 because of changes in get_class function in PHP 5.3.0

Categories