php public function is not working outside of the class [duplicate] - php

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! :)

Related

PHP5 - Error when calling class property as function [duplicate]

This question already has answers here:
How to call a closure that is a class variable?
(2 answers)
Closed 4 years ago.
$f = function($v) {
return $v + 1;
}
echo $f(4);
// output -> 5
The above works perfectly fine. However, I cannot reproduce this correctly when f is a property of a class.
class MyClass {
public $f;
public function __construct($f) {
$this->f = $f;
}
public function methodA($a) {
echo $this->f($a);
}
}
// When I try to call the property `f`, PHP gets confused
// and thinks I am trying to call a method of the class ...
$myObject = new myClass($f);
$myObject->methodA(4);
The above will result in an error:
Call to undefined method MyClass::f()
I think the problem is that it is trying to make sense of
echo $this->f($a);
And as you've found it wants to call a member function f in the class. If you change it to
echo ($this->f)($a);
It interprets it as you want it to.
PHP 5.6
Thanks to ADyson for the comment, think this works
$f = $this->f;
echo $f($a);
While Nigel Ren's answer (https://stackoverflow.com/a/50117174/5947043) will work in PHP 7, this slightly expanded syntax will work in PHP 5 as well:
class MyClass {
public $f;
public function __construct($f) {
$this->f = $f;
}
public function methodA($a) {
$func = $this->f;
echo $func($a);
}
}
$f = function($v) {
return $v + 1;
};
$myObject = new myClass($f);
$myObject->methodA(4);
See https://eval.in/997686 for a working demo.

How to share variables in php functions [duplicate]

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

How to get name of a initializer variable inside a class in PHP [duplicate]

This question already has answers here:
Get variable name of object inside the object php
(3 answers)
Closed 8 years ago.
I would like to know, if it is possible in PHP (using reflection or not) to get the variable name abc inside the class method in this example.
class Example
{
public function someMethod()
{
// once this method is called, I want it to echo `abc` in this example
}
}
Now, when I call the method using a variable name like
$abc= (new Example)->someMethod();
echo $abc; // abc
I would like to see the name of the variable, 'foo' shown, in other words the class would have to be aware of the variable name, when returning the methods contents.
I always pass in the name of the variable it will be assigned to if it it is required
class myclass {
var $myname;
function __construct($myname='no name') {
$this->myname=$myname;
#print "In BaseClass constructor\n";
}
function sayHello()
{
return "hello from " . $this->myname . "\n";
}
}
usage:
$myVar = new myclass("myVar");
$yourVar = new myclass("yourVar");
echo $myVar->sayHello();
echo $yourVar->sayHello();

php: can't create class variable from function [duplicate]

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

Using constructor and a public function in a class [duplicate]

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

Categories