class scopes in PHP [duplicate] - php

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

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

In php class object not working inside normal function? [duplicate]

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 7 years ago.
<?php
class Door
{
public function __construct()
{
}
public function test(){
echo "welocme";
}
}
$obj=new Door();
get_data();
function get_data(){
$obj->test();
}
$obj->test(); work well outside function but i need inside function. I cannot access object inside function show error
Fatal error: Call to a member function test()
Try like this: it may work..
If u use any outer variable in a function, then decleare as global $use_variable_name . now u can understand...
function get_data(){
global $obj;
$obj->test();
}
another and better way:
get_data($obj);// call this way...
function get_data($object){
$object->test();
}

why can i reach to subclass? [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
I have two class.
First class = database.class.php (sub)->vericek.class.php
Database.class.php is:
Class Database
{
public function __construct($class)
{
foreach($class as $class)
{
require_once("sub/" . $class . ".class.php");
$$class = new $class();
}
}
}
$database = new Database(array("vericek"));
$database->vericek->abc();
and vericek.class.php is:
Class vericek
{
public function abc()
{
echo "try";
}
}
I want to see "try".. but i can't..
I can see this error: Fatal error: Call to a member function abc() on a non-object in C:\AppServ\www\ozetizle\classes\database.class.php on line 32
How can i?
You'll need to assign it as $this->$class = ... because using $$class will be a local variable, just visible in your constructor.

Static array variable of another class' objects does not allow calling methods of the second class [duplicate]

This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
I am can't figure out why this doesn't work:
class Test
{
public static $arData=array();
public static function addMember(Person $member)
{
self::$arData[]=$member;
}
public static function showAll()
{
for($i=0;$i<count(self::$arData);$i++)
{
self::$arData[i]->show();
}
}
}
What I get is this: Fatal error: Call to a member function show() on a non-object.
The show() method does exist and it basically prints out name and location of a person.
In in the constructor, instead of adding $member to $arData I do $member->show() it works.
So... what's up?
Try
self::$arData[$i]->show();
How about this:
foreach (self::$arData as $person) {
$person->show();
}
The error is in the for-loop:
...
public static function showAll()
{
for($i=0;$i<count(self::$arData);$i++)
{
self::$arData[$i]->show();
}
}
...
It must be $i and not only i in the array-access-operator when calling the show()-method.

Categories