Array inside function not working? - php

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.

Related

php - how to call static class and function with variable?

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));

PHP Syntax Error in the Instance Declaration

i have a little syntax error which i'm not able to sort out, can anyone help ?
Syntax:
Config Class:
Error:
Do not instantiate private variables like that, you should only be using them for declaring properties and simple values.
You cannot declare a private variable (declaring them a return value from a static functions at least) like that, just do it in the constructor __construct() for the object. You will get the same error for any class you do with a private variable declaration like that and setting it as a return value for any function. Try running the below in PHPFiddle and you'll get the same error.
<?php
class A {
private $hi = B::some_function('hi');
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Instead do something like:
<?php
class A {
private $hi;
public function __construct() {
$this->hi = B::some_function('hi');
}
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Your syntax is incorrect as I've seen in that picture, simply because you didn't have a closing bracket '}' for the class User.
Just try this one.
Use semicolon for every function call as shown below,
$_table = Config::get('tables/users');
$_seassionsTable = Config::get('tables/user_sessions');
It may be fix your issue.

PHP 5.6.3 Parse error: syntax error, unexpected '[' in class

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!");
}

Is static array property not possible in php?

Below is my code in php,and I am getting error:
Parse error: syntax error, unexpected '[' in /LR_StaticSettings.php on line 4
<?php
class StaticSettings{
function setkey ($key, $value) {
self::arrErr[$key] = $value; // error in this line
}
}
?>
I want to use statically not $this->arrErr[$key] so that I can get and set static properties without creating instance/object.
Why is this error? Can't we create static array?
If there is another way, please tell me. Thanks
You'd need to declare the variable as a static member variable, and prefix its name with a dollar sign when you reference it:
class StaticSettings{
private static $arrErr = array();
function setkey($key,$value){
self::$arrErr[$key] = $value;
}
}
You'd instantiate it like this:
$o = new StaticSettings;
$o->setKey( "foo", "bar");
print_r( StaticSettings::$arrErr); // Changed private to public to get this to work
You can see it working in this demo.
Your code doesn't define $arrErr as a static member variable. You should declare it as
<?php
class StaticSettings{
public static $arrErr = array();
function setkey($key,$value){
self::arrErr[$key] = $value;
}
}
?>

add function to an array in php

I'm having trouble declaring an array in conjunction to a function. Here is my code, what am I doing wrong?
private function array_list(){
return array('1'=>'one', '2'=>'two');
}
private $arrays= array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
Getting unexpected T_VARIABLE error when I run this code.
You cannot declare arrays like this as property:
private $arrays= array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
You cannot use an array returned from a class method in the property definition.
You should populate it inside a constructor for example. Like this:
private $arrays = array();
public function __construct() {
$this->arrays = array(
'a'=>array('type'=>'1', 'list'=>$this->array_list())
);
}
Do it in a method, for example, the constructor:
class Foo {
function __construct () {
$this->arrays['list'] = $this->array_list ();
}
}

Categories