I have tried this code with and without {} but it keeps saying "syntax error, unexpected '{', expecting identifier (T_STRING) in .."
$page = $_GET['page'];
$do = $_GET['do'];
{$page}::{$do}();
Then I have a class and a function in that class that should get called which looks like this.
class Example {
public static function index()
{}
}
So if $page = "Example" and $do = "index" I want to call Example::index();
There are two ways you can do that:
//$class: the class which contains your static function
//$method: the function you want to call
call_user_func("$class::$method");
//OR
call_user_func(array($class, $method));
in your case:
call_user_func("$page::$do");
OR
call_user_func(array($page, $do));
Related
PHP CODE
class XXX{
public function ggGet($str){
return gGet($str); // This is ok working gGet is global function
}
public static $Array = array ( "value" => $this->ggGet("email")); // This code is error Why?
}
I must use a function in array in class.
I see this error.
Parse error: syntax error, unexpected '$this' (T_VARIABLE) in /var/www/html/
What must i do?
Thank you.
Try this:
class XXX{
$MyArray = array();
public function __construct(){
$this->MyArray["value"] = $this->ggGet("email");
}
public function ggGet($str){
return gGet($str);
}
}
Use __construct() every time you need to start values in a var inside a class.
I would like to "use" a static class variable as part as the use list statement of a closure ?
Following snippets simply fail as unexpected 'self' parse errors.
array_walk($_categories, function($c, $i) use (&self::$tree) {
OR
array_walk($_categories, function($c, $i) use (self::&$tree) {
Parse error: syntax error, unexpected 'self' (T_STRING), expecting variable (T_VARIABLE)
Is there any special syntax to use in this very special case ?
Why on earth would you want to do that? Given your use of self, the closure is clearly defined within the class somewhere, so you're able to access the static member anyway:
class Foo
{
protected static $bar = 123;
public function test()
{
return function($x) {
static::$bar += $x; // or self::$bar
return static::$bar;
};
}
}
$x = new Foo;
$y = $x->test();
var_dump($y(1));//int(124)
var_dump($y(2));//int(126)
No need to muck about with references at all...
If you're on an EOL'ed version of PHP (5.3 for example), you could work around the problem by assigning a reference to the static member first, and then pass a reference to that reference via use:
public function test()
{
$staticRef = &static::$bar;
return function($x) use (&$staticRef) {
$staticRef += $x;
return $staticRef;
};
}
But if you're still working with a version of PHP that was EOL'ed long ago, really you should be upgrading...
While I was creating a class in php, I experienced this error:
Parse error: syntax error, unexpected '[', expecting ',' or ';' on line 5
A simple example:
<?php
class MyClass
{
public $variable["attribute"] = "I'm a class property!";
}
?>
I already had a look at Reference - What does this error mean in PHP? but this doesn't seem to fit to my case. The problem of all other existing Questions seem to rely to an old PHP Version. But I am using PHP 5.6.3!
What can I do? Am I just sightless?
You can't explicitly create a variable like that (array index). You'd have to do it like this:
class MyClass {
// you can use the short array syntax since you state you're using php version 5.6.3
public $variable = [
'attribute' => 'property'
];
}
Alternatively, you could do (as most people would), this:
class MyClass {
public $variable = array();
function __construct(){
$this->variable['attribute'] = 'property';
}
}
// instantiate class
$class = new MyClass();
I guess you should declare it the way it is shown below :
class MyClass
{
public $variable = array( "attribute" => "I'm a class property!" );
}
Make an array first. Use the code below
<?php
class MyClass
{
public $variable = array("attribute"=>"I'm a class property!");
}
?>
HOpe this helps you
You cannot declare class members like this. Also you cannot use expressions in class member declarations.
There are two ways to achieve what you are looking for :
class MyClass
{
public $variable;
function __construct()
{
$variable["attribute"] = "I'm a class property!";
}
}
or like this
class MyClass
{
public $variable = array("attribute" => "I'm a class property!");
}
i have this code:
protected $val = Zend_Registry::get('values');
Whenever I put this piece of code I get:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...
Why is it happening?
You cannot use a function call or other dynamic expression to initialize a class property. It can only be a constant or atomic value. If you need to initialize it with a function call, you must do this instead inside the constructor.
protected $val = NULL;
public function __construct() {
$this->val = Zend_Registry::get('values');
}
From the docs:
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.
You can not use the return-value of a function for the initial value of a class-variable.
You can however set it in the constructor of the class.
class Myclass{
protected $val;
public function __construct(){
$this->val = Zend_Registry::get('values');
}
}
Because that looks like a class variable and you cant assign data to a class variable like that.
See here http://www.php.net/manual/en/language.oop5.properties.php
You could do it like this.
class something {
protected $_val;
public function __construct()
{
$this->_val = Zend_Registry::get('values');
}
}
I have a class like
class blah extends blahblah{
private $variable = '5';
function somefunction(){
echo $variable;
}
}
this works in php 5, but not in php 4.
I get a error:
Parse error: parse error, unexpected
T_VARIABLE, expecting T_OLD_FUNCTION
or T_FUNCTION or T_VA....
I also tried with public and static. Same error.
How can I add a variable inside that class that I can access from all class functions?
private is not a valid keyword in PHP 4 change it to var $variable = '5';
also the function is wrong it should be...
class blah extends blahblah{
var $variable = '5';
function somefunction(){
echo $this->variable;
}
}
In PHP4, member variables are declared with var:
var $variable = '5';
But you still have to access it via $this->variable in your function (I think, I'm not so familiar with PHP4).
That said, if possible, upgrade! PHP4 and "OOP" is more pain than fun.
Update: Ha, found it, some documentation about Classes and Objects in PHP4.