the class below is part of an mvc structure im building for my website. the class is there to produce a page if index.php is called without any request in the url. if that is good practice or not I do not know. Im new to php, and this is the first time i implement an mvc structure.
<?php
class Default_Model
{
private $defaultPage = array
(
'headline' => 'JOBBSĂ–KAREN',
'instruction' => 'logga in nedanför'
);
public function __construct()
{
}
public function getContent()
{
line 17 return $defaultPage;
}
}
?>
here is the error message:
Notice: Undefined variable: defaultPage in /home/mengus/dev/www/models/default.php on line 17
so what I dont get is why the array is undefined. I use similar code in other classes and it works just fine. is it a scope issue? I seem to have gone error blind from staring at this :)
thanks for help.
To access the fields you need to use $this and -> operator
return $this->defaultPage;
Related
I need to call some functions inside a class; based on variables, something like this:
$x->$y();
But, I found a strange behavior, consider the following sample code:
$arr = array(
"some_index" => "func_name"
);
$str = "func_name";
class some_class {
public function func_name() {
echo "It works in class!";
}
}
$some_obj = new some_class();
$some_obj->$arr['some_index']();
$some_obj->$str();
Now the line
$some_obj->$arr['some_index']();
gives the errors:
Array to string conversion in ...
Undefined property: some_class::$Array in ...
Uncaught Error: Function name must be a string in...
But, the line
$some_obj->$str();
works perfectly.
Also, both the lines will work, if the function is not defined inside a class.
Anyone knows why this is happening ?
You should call it this way:
$some_obj->{$arr['some_index']}();
here's a living example
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']);
}
I am new to php so please excuse my lack of knowledge. I am using eclipse and have a project with 3 files inside the project. I am creating a find discount class which takes the class object to call a function from another class. The error:
Notice: Undefined variable: GetInfoClass line ..
Fatal error: Call to a member function getAge() on a non-object line ...
I tried to read about it but I cant seem to understand it. Please help. Thanks
formResponse:
include "GetInfo.php";
include "IfDiscount.php";
$IfDiscount= new IfDiscount();
echo $IfDiscount->findDiscount();
class IfDiscount:
class IfDiscount
{
public function findDiscount(){
$Age = $GetInfoClass->getAge();
echo $Age;}}
$GetInfoClass is not available to findDiscount() because it is out of scope. You should pass it to findDiscount() as a parameter to make it available to that method:
public function findDiscount($GetInfoClass){
$Age = $GetInfoClass->getAge();
return $Age;
}
echo $IfDiscount->findDiscount($GetInfoClass);
(You also want to return $Age, not echo it. You already explicitly echo it when you call that method.)
I have a problem in calling a function that the name is being stored in an array.
class tempClass {
function someFunction() {
$tempArray = array('functionName' => 'tempFunction');
$tempArray['functionName']();
}
function tempFunction() {
echo "inside function";
}
}
It gives me an error:
"Fatal error: Call to undefined function tempFunction() in /..... line..".
Line number is the line where the function is being called, $tempArray['functionName']();
But if called the method_exists(), it shows that the method is exists. It is very confusing. Can anyone please help me out? Thanks.
Use call_user_func() , like this:
call_user_func($tempArray['functionName']);
UPDATE:
As you want to call a method of a class from inside that class, use the following instead:
call_user_func(array($this, $tempArray['functionName']));
See working demo
Well you ask if the method exists inside the class or object, but you call it without that scope. That won't work...
Try this instead:
call_user_method($tempArray['functionName'],$this);
Just saw that call_user_method() is depreciated. Use call_user_func() as answered by Nelson instead.
Having problems accessing session variables through different actions in ZF.
Here are the contents of the bootstrapper:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initSession()
{
Zend_Session::start();
$sessionUser = new Zend_Session_Namespace('sessionUser');
}
}
I assign a variable to $sessionUser in the IndexController:
$sessionUser->userId = $res->userId;
$sessionUser->organisationId = $res->organisation_organisationId;
$sessionUser->forename = $res->forename;
And attempt to access the variable in the Administrator controller:
$sessionUser->organisationId
Yet I receive the error:
Notice: Undefined variable: sessionUser in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17 Notice: Trying to get property of non-object in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17
And ideas what could be causing this?
Many thanks
To get the session variable back in your controller you also need to do:
$sessionUser = new Zend_Session_Namespace('sessionUser');
Well, the error you are getting is obvious. The $sessionUser is not defined.
You must initialize such variable before assinging values to it. Put this in your controller:
$sessionUser = new Zend_Session_Namespace('sessionUser');