Fatal error: Using $this when not in object context [duplicate] - php

This question already has answers here:
PHP Fatal error: Using $this when not in object context
(9 answers)
Closed 7 years ago.
What's going wrong? Before I used static properties and methods and self::, but I now i don't need it. Don't know where is mistake.
class Main_PopupTemplate
{
public $arg = null;
public function setMark($k) {
$this->arg = func_get_arg(0);
}
public function getMark() {
$discTexts = $this->getArg();
$result = isset($discTexts[$this->arg]) ? $discTexts[$this->arg] : null;
return $result;
}
public static function getArg()
{
return array(
'disclaimer-01' => 'Text-1',
'disclaimer-02' => 'Text-2',
'disclaimer-03' => 'Text-3',
'disclaimer-04' => 'Text-4'
);
}
}

Issue is solved. Method call was static :)
$selectPopupText = new Main_PopupTemplate;
$selectPopupText->setMark('disclaimer-03');

Related

why [ ] notation is used to input element to static array [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
in the original example $methods is a static array.It is used to assign outsider functions as methods to a class.But to input new element to this array writer used the following expression
static protected $methods = array();
i removed the static keyword from the variable declaration and tried to execute the code.
protected $methods = array();
the execution gave the following error:
Fatal error: Access to undeclared static property: Dynamic::$methods
in C:\xampp\htdocs\practice\json.php on line 6
what is this [ ] notation is used for and how it is related to static keyword??
original full code:
class Dynamic {
static protected $methods = array();
public static function registerMethod($method) {
self::$methods[] = $method;
}
private function __call($method, $args) {
if (in_array($method, self::$methods)) {
return call_user_func_array($method, $args);
}
}
}
function test() {
print "Hello World" . PHP_EOL;
}
Dynamic::registerMethod('test');
$d = new Dynamic();
$d->test();
what is this [ ] notation is used for and how it is related to static keyword??
The use of static and [] are unrelated.
The code $array[] = $i is a shorthand for pushing the element $i onto the end of the array $array. It could also be written array_push($array, $i).

class scopes in PHP [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 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

php call another function in a function [duplicate]

This question already has answers here:
Fatal error: Using $this when not in object context
(4 answers)
Closed 8 years ago.
im trying to call a function inside another function. based on some research they say using
$this->
should work. but it gave me
Fatal error: Using $this when not in object context
function addstring($input, $addition_string , $position) {
$output = substr_replace($input, $addition_string, $position, 0);
return $output;
}
function test($astring) {
$output2 = $this->addstring($astring, 'asd', 1);
}
to view the rest of my code :
http://pastebin.com/5ukmpYVB
error :
Fatal error: Using $this when not in object context in BLA.php on line 48
$this-> is needed if you are within a class, if you don't, just call the function by its name:
function test($astring) {
$output2 = addstring($astring, 'asd', 1);
}
Besides the error mentioned by Nicolas,
function test($astring) {
has no return value and does not use the parameter by reference, which means, the function is not doing anything but wasting performance.
To demonstrate how you bring the functions into class context:
class StringHelper
{
private $output;
protected function addstring($input, $addition_string , $position) {
$output = substr_replace($input, $addition_string, $position, 0);
return $output;
}
public function test($astring) {
$this->output = $this->addstring($astring, 'asd', 1);
return $this;
}
public function getOutput() {
return $this->output;
}
}
$stringObj = new StringHelper;
echo $stringObj->test('my string')->getOutput();

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