I must use PHP pack() function to do something in my codes. But when I do it inside a class, I get the errors below. Whereas if I use it normally (without using in class), it works without any error.
class encryption
{
$my_key = pack('H*', "123456");
}
and this is the error:
Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in ...
Also if I use public for $my_key, I will get a new error.
class encryption
{
public $my_key = pack('H*', "123456");
}
Error:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...
What is wrong in my codes?
When defining a variable for a class outside of a function, you can't use another function to define it. If you need it to have that value when initialised, set the value in the constructor function. Your class will then look like :
class encryption
{
public $my_key;
public function __construct() {
$this->my_key = pack('H*', "123456");
}
}
class encryption
{
public $my_key;
public function __construct()
{
$this->my_key = $this->pack('H*', "123456");
// will always be executed when you create an object of this class
}
private function pack($arg1, $arg2)
{
// return something
}
}
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.
If I create a simple php class that looks like this:
<?php
class Test {
public $dir = __DIR__.__FILE__;
public function __construct() {
echo $this->dir;
}
}
new Test();
When the script runs, it fails with a fatal error:
Parse error: parse error, expecting `','' or `';'' in Test.php on line 4
However, if I remove the concatenation, for example public $dir = __DIR__;, the script works. Why would concatenating magic constants in this way cause a fatal error?
I am trying to access a static class member variable in an array.
My Code (index.php):
<?php
class Foo
{
public static $staticVar = 'test';
}
class Bar
{
public $someArray = array(
Foo::$staticVar
);
}
$cls = new Bar();
var_dump($cls->someArray);
?>
On PHP-7.0 I get this error:
PHP Fatal error: Constant expression contains invalid operations in
/var/www/html/index.php on line 12
On PHP-5.6 I get this error:
PHP Parse error: syntax error, unexpected '$staticVar' (T_VARIABLE),
expecting identifier (T_STRING) or class (T_CLASS) in
/var/www/html/index.php on line 11
I just want to have the string "test" in my array.
It´s strange that when I 'echo' out the variable it works as expected:
echo Foo::$staticVar // prints 'test'
I´m new to PHP and I can´t figure out what I´m doing wrong.
Unfortunately, you can't refer to another variable or class in the initial declaration of a class property. It's just a limitation of the language as it stands. The general workaround is to initialise the property in the constructor, e.g.
class Bar
{
public $someArray = array();
public function __construct()
{
$this->someArray = array(
Foo::$staticVar
);
}
}
On a vaguely related note, PHP 5.6 did at least make some vague headway in allowing you to define constants as basic expressions, see https://3v4l.org/6TDZV
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');
}
}