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.
Related
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!");
}
Braking my head over this.
I’m trying to assign a date() function to a property of foo() class like that.
class Foo{
public $dt = date("F-d-Y H:j:s");
function today(){} //just some empty method
};
$g = new Foo();
echo $g->dt;
I get Parse error: syntax error, unexpected '(', expecting ',' or ';' //this is date() line
I also tried that.
class Foo{
public $dt;
function today(){
echo $this->dt = date("F-d-Y H:j:s");
}
};
$g = new Foo();
$g->today();
No errors and everything works fine. So, seems to me, that we can't assign date() to a class property directly.
Please point me to a right direction. Thanks in advance.
If you provide an initialization of a class property, it has to be a constant, not a function call. If you want to compute the initial value, do it in the class constructor method.
class Foo {
public $dt;
function __construct() {
$this->dt = date("F-d-Y H:j:s");
}
}
$g = new Foo();
echo $g->dt;
From the PHP documentation:
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.
Although it is allowed to access class variables using the syntax:- $object::$variable, does it hold any significance i.e. in case of accessing class variables we can use either the classname or an object of that class to access a class variable isn't it?
This operator is used to access static variables. This means that the variable is linked to the class, and not to an instance of that class. i.e. shared over all instances.
here's an example to show you what I mean:
class MyClass
{
public static $myStaticVar;
public $myObjectVar;
}
$instance1 = new MyClass();
$instance2 = new MyClass();
// normal vars are linked to an instance of a class
$instance1->myObjectVar = 'value1';
$instance2->myObjectVar = 'value2';
// statics are shared between all instances of the same class
$instance1::$myStaticVar = 'value3';
echo $instance2::$myStaticVar; // results in 'value3'!
Do you mean this?
<?php
class A {
public static $b = 'Hello World!';
}
echo A::$b;
$obj = new A();
echo $obj::$b;
Warning: I do not recommend to access a static class member using an instance variable.
This only works for PHP >= 5.3.0.
PHP <= 5.2.17 doesn't like $obj::$b and throws a syntax error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM,
expecting ',' or ';'
I would like to know if there is anyway I can make a parent object with php, I have tried this:
new parent::__construct($var);
but it doesn't work and I get the following error in the php logs:
(..)PHP Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE or '$'(..)
see http://uk.php.net/get_parent_class
<?php
class Foo {
}
class Bar extends Foo {
protected $p;
public function __construct() {
$pc = get_parent_class();
$this->p = new $pc;
}
}
$bar = new Bar;
var_dump($bar);
(But somehow I fail to see why you would need something like that. But maybe that's just me.... ;-))
Just call the constructor of the parent class like:
$parentClassObject = new ParentClassName();
Use parent::__construct() to call the parent class constructor since it is not done automatically in PHP.
This might work:
$parentClass = get_parent_class();
$parentObject = new $parentClass();