This question already has an answer here:
Can't initialize a PDO object in a class as a property [duplicate]
(1 answer)
Closed 9 years ago.
sorry for the noobish question.
I'm new with PHP class programming and I can't figure out why this piece of code doesn't work:
class Job {
private $var1 = 'hi there';
private $var2 = date('Y/m/d');
public function foo() { /* some code */ }
}
$job = new Job();
I get parse error parse error, expecting','' or ';'' generated by $var2.
Looks like I can't initialize a variable inside a class from a PHP function.
How can I bypass this error?
Thank in advance.
Initialize it from within the constructor:
class Job {
private $var1 = 'hi there';
private $var2 = null;
public function __construct() { $this->var2 = date("Y/m/d"); }
public function foo() { /* some code */ }
}
$job = new Job();
Related
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
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have created a simple class, variable, and public function. When I try to initiate an object and call the public function from the class outside of the class, I get no output that I can see.
<?php
error_reporting(E_ALL);
ini_set("error_reporting", 1);
class Game {
var name;
public function show_game() {
echo $this->name;
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
From what I understand, the function should echo out testing game. But I am not seeing any output when I load up the page.
var name;
Is not valid syntax, change to:
var $name;
You seem to have simply forgotten the $ in your PHP variable. var name should be var $name:
<?php
class Game {
var $name;
public function show_game() {
echo $this->name; // Returns "testing game"
}
}
$game = new Game;
$game->name = "testing game";
$game->show_game();
?>
Hope this helps! :)
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
This is an outline of a problem which I'm struggling to solve in my code. I guess my knowledge of scope isn't that great.. I don't understand why the function getGreeting is giving a parse error.
<?php
class Class_1 {
public $t;
public function __construct() {
$this->t = "hello world";
}
public function helloWorld() {
return $this->t;
}
}
$x = new Class_1();
function getGreeting() {
return $x->helloWorld();;
}
echo getGreeting();
?>
the error I get is:
Fatal error: Call to a member function helloWorld() on a non-object.
Because you need to initialize object in the function to access it's methods from it :
function getGreeting() {
$x = new Class_1();
return $x->helloWorld();;
}
Example
This question already has answers here:
When would you use the $this keyword in PHP?
(12 answers)
Closed 9 years ago.
I tried to create a basic class with a function. Once an instance of the class is created new BaseClass("hello") then the constructor function saves the parameter to a variable.
The ->ret_text() should return with this variable, but it doesn't works. The error is: unexpected T_OBJECT_OPERATOR
class BaseClass {
var $txt;
function __construct($text) {
$txt = $text;
}
public function ret_text() {
return $txt;
}
}
echo (new BaseClass("hello"))->ret_text();
You should access your class variables with $this->variableName where $this refers to the class you are in.
In your example $txt is not the class variable $txt but only a variable for the current function (__construct(), ret_text() or something else). Also you can't call a method directly after the initialization of the class, i.e. (new Class())->methodName(); will not work for PHP version < 5.4. However, it will work for PHP version => 5.4.
Instead try this:
class BaseClass {
var $txt;
function __construct($text) {
$txt = 'This is a variable only for this method and it\'s not $this->txt.';
$this->txt = $text;
}
public function ret_text() {
$txt = 'This is a variable only for this method and it\'s not $this->txt.';
return $this->txt;
}
}
$bc = new BaseClass("hello");
echo $bc->ret_text();
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];
}