Slim Framework member function render() on a non-object [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
So I'm refactoring my code to implement more OOP. I set up a class to hold page attributes.
class PageAtrributes
{
private $db_connection;
private $page_title;
public function __construct($db_connection)
{
$this->db_connection = $db_connection;
$this->page_title = '';
}
public function get_page_title()
{
return $this->page_title;
}
public function set_page_title($page_title)
{
$this->page_title = $page_title;
}
}
Later on I call the set_page_title() function like so
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
When I do I receive the error message:
Call to a member function set_page_title() on a non-object
So what am I missing?

It means that $objPage is not an instance of an object. Can we see the code you used to initialize the variable?
As you expect a specific object type, you can also make use of PHPs type-hinting featureDocs to get the error when your logic is violated:
function page_properties(PageAtrributes $objPortal) {
...
$objPage->set_page_title($myrow['title']);
}
This function will only accept PageAtrributes for the first parameter.

There's an easy way to produce this error:
$joe = null;
$joe->anything();
Will render the error:
Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23
It would be a lot better if PHP would just say,
Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.
Usually you have build your class so that $joe is not defined in the constructor or

Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.

It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.
IE
$game = new game;
$game->doGameStuff($gameReturn);
foreach($gameArray as $game)
{
$game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}
$game->doGameStuff($gameReturn); // Wont work because $game is declared as a standard variable. You need to be careful when using common variable names and were they are declared in your code.

function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
looks like different names of variables $objPortal vs $objPage

I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.
$objPage = new PageAtrributes;
function page_properties() {
global $objPage;
$objPage->set_page_title($myrow['title']);
}

I realized that I wasn't passing $objPage into page_properties(). It works fine now.

you can use 'use' in function like bellow example
function page_properties($objPortal) use($objPage){
$objPage->set_page_title($myrow['title']);
}

Related

Passing parameters in a function inside another 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 6 years ago.
I m searching for some help
It is possible to set a function with Arguments/Parameters inside another function in php? Here i have a theoretical example.
<?php
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
When i try to set the parameter like this
funcOne('Alex');
echo funcTwo();
I get the following notice error
Notice: Undefined variable: arg in...
Thanks in advance :)
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
funcOne('Alex');
//Call is made to function one which returned an ARG.
NOTE that here the function just returned the arg and forgot about it, now that argument is NO WHERE stored to be used
//Now here inside the functionTwo scope $arg is never defined.
echo funcTwo();
You may do the following using classes and objects
class MyClass {
public $classarg;
public function funcOne($arg) {
$this->classarg = $arg; //assigned the argument to a class variable
}
function funcTwo() {
return $this->classarg; //using the class variable to test
}
}
$myobj = new MyClass();
$myobj->funcOne('Alex');
echo $myobj->funcTwo()
You can also use global variable to achieve what you want, but I will NOT recommend to use it as Object Oriented Programming is what we should be using going forward
funcOne('Alex')is not setting a parameter, it is calling the function funcOne().
When funcOne($arg) executes, it returns the parameter $arg to the caller.
echo funcOne('Alex') will echo Alex, because that is the value returned.
After return, funcOne does not know about 'Alex' any more.
when you call funcTwo(), it executes funcOne($arg), but $arg is not defined: it has no value assigned.
function funcTwo($arg) {
return funcOne($arg);
}
Note that you should learn to use variables before making functions.

Why a global variable isn't recognized?

I want to call for a variable defined at a some point in the code. I use global keyword but it seems variable isn't recognized. When I set variable locally it works just fine. (it is the $title variable, it receives value of a static function of some object)
THIS ONE WORKS:
class Book {
public function represent() {
$titles = Title::all_by_id();
$title = $titles[$this->title_id];
return $title->represent().'_'.$this->id;
}
}
THIS ONE DOESN'T:
$titles = Title::all_by_id();
in another file
class Book {
public function represent(){
global $titles;
$title = $titles[$this->title_id];
return $title->represent().'_'.$this->id;
}
}
It sends an error:
PHP Fatal error: Call to a member function represent() on a non-object in
What are possible problems here?
I solved it, it was the problem that I set the $titles variable in a different subcontext of the main context than where Book class was defined
I solved the problem when I changed the place where I was defining that variable, I've put it in the main context.

Error when using file_get_contents for class property

I get an error saying unexpected '(' when trying to do this in a class:
private $doglist = file_get_contents('dogdropdown.html');
Is that not allowed for some reason?
I also tried using a function like this:
public function getDogList(){
$list = file_get_contents('dogdropdown.html');
return $list;
}
which also didnt work. if I used include it does but doesnt inlcude it in the right place.
When you declare a class property you can only assign basic scalar values or null for variables that should reference objects. If the property needs to hold the result of some operation you either make it static or assign the result either in the constructor or a method of the class.
In order to do what you are trying to do you need the following:
class MyClass {
private $doglist;
function __construct() {
$this->doglist = file_get_contents('dogdropdown.html');
}
}

callback invoked in class extension

I'm working my way through php callbacks. I think I've covered the basics, but when working within class extensions I'm still hitting a brick wall somewhere for some time now, therefore it is time for some stack overflow rubber duck programming...!
Calling a callback from a class extension:
class myClass {
function cb_check($function) {
call_user_func($this->$function);
}
static function myFunction() {
var_dump("Hello World");
}
}
class myExtClass extends myClass {
function cb_invoke() {
$this->cb_check('myFunction');
}
}
$x = new myExtClass;
$x->cb_invoke();
Error message (or, notice and warning):
Notice: Undefined property: myExtClass::$myFunction in F:\test.php on
line 5
Warning: call_user_func() expects parameter 1 to be a valid callback,
no array or string given in F:\test.php on line 5
Line 5 is the call_user_func() above.
Does anybody have an idea what I am missing here?
Thank you in advance!
As mark pointed out,
call_user_func($this->$function);
should be replaced by either
call_user_func(array($this, $function));
or
call_user_func('self::' . $function);
Thanks, Mark!
While working with static classes you should have to use scope resolution operator :: you can not create the instance directly .
$x::myExtClass;
Try it.
Your error arises because you've declared myFunction() to be static. Static methods don't belong to any specific instance of a class and therefore can't be accessed through the $this variable. Instead you use self::myFunction() to call the static method. The easiest way to make this happen is to build the string of the static method call and pass the string as the parameter to call_user_func(). Your code would work as-written if myFunction() were not static.
class myClass {
function cb_check($function) {
// Build a static-method-style string
call_user_func('self::' . $function);
}
static function myFunction() {
var_dump("Hello World");
}
}
class myExtClass extends myClass {
function cb_invoke() {
$this->cb_check('myFunction');
}
}
$x = new myExtClass;
$x->cb_invoke();

Calling member function from other member function in PHP?

I'm a little confused about the situation shown in this code...
class DirEnt
{
public function PopulateDirectory($path)
{
/*... code ...*/
while ($file = readdir($folder))
{
is_dir($file) ? $dtype = DType::Folder : $dtype = Dtype::File;
$this->push_back(new SomeClass($file, $dtype));
}
/*... code ...*/
}
//Element inserter.
public function push_back($element)
{
//Insert the element.
}
}
Why do I need to use either $this->push_back(new SomeClass($file, $dtype)) or self::push_back(new SomeClass($file, $dtype)) to call the member function push_back? I can't seem to access it just by doing push_back(new SomeClass($file, $dtype)) like I would have expected. I read When to use self over $this? but it didn't answer why I need one of them at all (or if I do at all, maybe I messed something else up).
Why is this specification required when the members are both non-static and in the same class? Shouldn't all member functions be visible and known from other member functions in the same class?
PS: It works fine with $this-> and self:: but says the functions unknown when neither is present on the push_back call.
$this->push_back will call the method as part of the CURRENT object.
self::push_back calls the method as a static, which means you can't use $this within push_back.
push_back() by itself will attempt to call a push-back function from the global scope, not the push_back in your object. It is not an "object call", it's just a plain-jane function call, just as calling printf or is_readable() within an object calls the usual core PHP functions.
I cant seem to access it just by doing push_back(new SomeClass($file, $dtype)) like I would have expected.
This way you call push_back() as a function. There is no way around $this (for object methods) or self::/static:: (for class methods), because it would result into ambiguity
Just remember: PHP is not Java ;)
You can access like this
public static function abc($process_id){
return 1;
}
public static function xyz(){
$myflag=self::abc();
return $myflag;
}
output : 1

Categories