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');
}
}
Related
no matter where i search, i never found any rule that said class's property can't be an Array, but no matter how i tried, it never worked. It is against the rule to assign ARRAY as class's property? If so, is there any workaround? Here is my code
class Imperials{
protected $Data;
function __Construct($passedData){
$this->$Data = $passedData;
echo($this->$Data['Name']);
}
}
$var = new Imperials(array('Name'='Buster','Race'='Nords'));
It would returned an error message
Fatal error: Uncaught Error: Cannot access empty property
Use $this->Data without the $ instead of $this->$Data and use => for the array.
class Imperials{
protected $Data;
function __Construct($passedData){
$this->Data = $passedData;
echo($this->Data['Name']);
}
}
$var = new Imperials(array('Name'=>'Buster','Race'=>'Nords'));
Say that I have a class like this:
class MyClass {
public $variable;
public function __construct($variable) {
$this->variable = $variable;
}
}
and I call it like this (without initializing it, or as far as php is concerned it's not even a class):
$my_class = null;
$v = $my_class->variable;
Is this allowed in php? It would give a big fat null pointer exception in most other languages. If it works, what's the value of variable?
It is "sort of" allowed - you will get a Notice: Trying to get property of non-object and the result will be NULL.
I anyway don't see a point of doing that.
That doesn't make sense...
If you declare a variable $my_class, and makes it NULL, every attribute given to that variable is null.
Of course, for php you're not doing anything wrong. You're just declaring that a variable is null.
This would be different if you declare $my_class as an object of your class MyClass, because you must give an attribute, even if this is null
class MyClass
{
public static $variable;
public function __construct($variable)
{
$this->variable = $variable;
}
}
\MyClass::$variable = null;
echo \MyClass::$variable;
i have a little syntax error which i'm not able to sort out, can anyone help ?
Syntax:
Config Class:
Error:
Do not instantiate private variables like that, you should only be using them for declaring properties and simple values.
You cannot declare a private variable (declaring them a return value from a static functions at least) like that, just do it in the constructor __construct() for the object. You will get the same error for any class you do with a private variable declaration like that and setting it as a return value for any function. Try running the below in PHPFiddle and you'll get the same error.
<?php
class A {
private $hi = B::some_function('hi');
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Instead do something like:
<?php
class A {
private $hi;
public function __construct() {
$this->hi = B::some_function('hi');
}
}
class B {
public static function some_function(string) {
return $string;
}
}
?>
Your syntax is incorrect as I've seen in that picture, simply because you didn't have a closing bracket '}' for the class User.
Just try this one.
Use semicolon for every function call as shown below,
$_table = Config::get('tables/users');
$_seassionsTable = Config::get('tables/user_sessions');
It may be fix your issue.
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 have this code:
protected $val = Zend_Registry::get('values');
Whenever I put this piece of code I get:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in ...
Why is it happening?
You cannot use a function call or other dynamic expression to initialize a class property. It can only be a constant or atomic value. If you need to initialize it with a function call, you must do this instead inside the constructor.
protected $val = NULL;
public function __construct() {
$this->val = Zend_Registry::get('values');
}
From the docs:
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
You can not use the return-value of a function for the initial value of a class-variable.
You can however set it in the constructor of the class.
class Myclass{
protected $val;
public function __construct(){
$this->val = Zend_Registry::get('values');
}
}
Because that looks like a class variable and you cant assign data to a class variable like that.
See here http://www.php.net/manual/en/language.oop5.properties.php
You could do it like this.
class something {
protected $_val;
public function __construct()
{
$this->_val = Zend_Registry::get('values');
}
}