I got a function who gets all the labels out of the database and puts them into an array, but if I take the object to another function it will be NULL
private static $labels='blaat';
public function loadDatabaseLabels(){
$res=DB::query("SELECT * FROM labels");
$label_array=array();
while($row=mysqli_fetch_assoc($res)){
$label_array[$row['indicator']]=$row['text'];
}
$labels = new label();
$labels->labels=$label_array;
}
public function getLabel($indicator){
var_dump($labels->label);
}
It seems that this is a code snippet from a PHP class. If you set a variable as $labels it's in the local method scope, is not seen by anything outside the method and gets lost after method finishes.
To set variables on the object instance, use $this->labels = ... and var_dump($this->labels), or as it's declared static, self::$labels, instead.
If that's not the case then forget about static public private keywords. Functions would still not see anything outside of them tho, so you have an ugly option of adding global $labels in front of the methods (karma will get back at you for this) or passing $labels bu reference to both methods as &$labels.
A few issues here. First, $labels is declared private static - this means to access it you would do so using self::$labels:
// set like this:
self::$labels->labels = $labels_array;
public function getLabel($indicator){
var_dump(self::$labels->label);
}
Secondly, you are not setting self::$lables->label, but self::$labels->labels (note the plural labels). So in the above function, var_dump is accessing something that hasn't been set.
Related
I am working on a PHP class which looks like this:
<?php
class xyz{
public $car1;
public $car2;
private $owner;
public function __construct ($type){
$this->car1 = $type;
$this->owner = "John";
return $this->owner();
}
private function owner(){
return "Owner of ".$this->car1." is ".$this->owner;
}
Now, here's the problem when I call this class via other code, I can easily access private variable and the return function is not working correctly.
Here's the sample:
<?php
$car = new xyz("Sedan");
echo $car; //Expected result: Owner of Sedan is John.
?>
If I print $car, Here's what I get
Object ( [car1] => Sedan [car2] => "" [owner:xyz:private] => John )
How can I achieve my desired results and How can I protect private variable?
All the helps and suggestions will be appreciated.
Thanks!
Constructors are not supposed to return any value. The class constructor is supposed to initialize an object when creating a new instance (e.g. when you write $car = new xyz("sedan");, so anything you return goes nowhere. Create other methods in the class to return values.
If you want to echo the owner, make the owner method public and do `echo $car->owner();". The method returns a string, and then the string is echoed. Simple.
Echoing the object directly should result in an error in php 7, maybe you are running an older version of php that returns what you've seen, which is what happens if you call var_dump($car);. If you want to control how an object is converted into a string, you need to override the __toString method (see the php documentation).
Properties and methods visibility keywords are working fine, if you try to use $car->owner or $car->owner() without changing visibility you should see errors.
On my site at the beginning of every script I include a "bootstrap" script which queries a few things from the database, does some calculations and then loads the variables into constants that I define one by one.
Some examples are:
define("SITE_ID", $site_id); // $site_id is pulled from a field in the database
define("SITE_NAME", $site_name);
// pulled from a field in the same row as the above
define("STOCK_IDS", $stock_ids);
//computed array of stock id integers from a different query.
//I perform logic on the array after the query before putting it in the definition
define("ANALYTICS_ENABLED", false);
// this is something I define myself and isnt "pulled" from a database
Now, I have many functions on the site. One example function is get_stock_info. And it references the STOCK_IDS constant.
What I want to do is have a class which has the above constants in it and the get_stock_info function.
Would the best approach to be have an empty class "site", create an instance of it and then afterwards define the static variables above one by one? Or is that not a good way and should I move all of of my logic which pulls from the database and calculates SITE_ID, STOCK_IDS, ANALYTICS_ENABLED etc into the constructor instead?
Ultimately I want the class to contain all of the above info and then I would be able to use class methods such as site::get_stock_info() and those methods will have access to the constants via self:: or this.
There's a lot more I want to do than that but that would give me enough to figure the rest out.
I think this approach isn't the best. You should consider not using constants as your values aren't constant. For your case it is better to have a class with classic getters methods.
Something like this:
class SiteInfo
{
private $siteId;
private $siteName;
private $stockIds;
private $analyticsEnabled;
public function __construct()
{
// Results from the database
$results = $query->execute();
$this->siteId = $results['siteId'];
$this->siteName = $results['siteName'];
$this->stockIds = $results['stockIds'];
$this->analyticsEnabled = $results['analyticsEnabled'];
}
public function getSiteId()
{
return $this->siteId;
}
public function getSiteName()
{
return $this->siteName;
}
public function getStockIds()
{
return $this->stockIds;
}
public function isAnalyticsEnabled()
{
return $this->analyticsEnabled;
}
}
The I18n class in CakePHP provides this method to create instances:
public static function getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] = new I18n();
}
return $instance[0];
}
Among other considerations (please correct me if I'm wrong), I understand it helps to use class instances from the convenience functions:
/**
* Returns a translated string if one is found; Otherwise, the submitted message.
*/
function __($singular, $args = null) {
// ...
$translated = I18n::translate($singular);
// ...
}
echo __('Hello, World!');
This looks cleaner than having to pass the instance around as argument (or, even worse, using a randomly named global variable). But I can't imagine a reason why $instance is an array rather than a plain object.
What can be the purpose of using a one-item array to store class instances?
I would suspect this to be leftovers from older PHP4/CakePHP versions where the instances were assigned by reference.
https://github.com/cakephp/cakephp/blob/1.2.0/cake/libs/i18n.php
function &getInstance() {
static $instance = array();
if (!$instance) {
$instance[0] =& new I18n();
$instance[0]->l10n =& new L10n();
}
return $instance[0];
}
$_this =& I18n::getInstance();
Assigning by reference doesn't work with static, the reference is not being remembered, but it works when assigned to an array entry.
So this was most probably just a workaround for a PHP limitation.
One possible reason for this is to keep all singleton class instances in one global - (static is a synonym of global in this case) array variable for monitoring or not messing the global/local namespace with individual variables for each singleton. If each of the static variables were with random names e.g $translated it would be more easier to overwrite and mess its value. - bug again for me, this is extremely rear possibility.
For example the I18Nn instance would be with [0] key, other class would have other key. You should check outher singleton classes how manage the static $instance array values.
How to combine two variables to obtain / create new variable?
public $show_diary = 'my';
private my_diary(){
return 1;
}
public view_diary(){
return ${"this->"}.$this->show_diary.{"_diary()"}; // 1
return $this->.{"$this->show_diary"}._diary() // 2
}
both return nothing.
Your class should be like following:
class Test
{
public $show_diary;
function __construct()
{
$this->show_diary = "my";
}
private function my_diary(){
return 707;
}
public function view_diary(){
echo $this->{$this->show_diary."_diary"}(); // 707
}
}
It almost looks from your question like you are asking about how to turn simple variables into objects and then how to have one object contain another one. I could be way off, but I hope not:
So, first off, what is the differnce between an object and a simple variable? An object is really a collection of (generally) at least one property, which is sort of like a variable within it, and very often functions which do things to the properties of the object. Basically an object is like a complex variable.
In PHP, we need to first declare the strucutre of the object, this is done via a class statement, where we basicaly put the skeleton of what the object will be into place. This is done by the class statement. However, at this point, it hasn't actually been created, it is just like a plan for it when it is created later.
The creation is done via a command like:
$someVariable= new diary();
This executes so create a new variable, and lays it out with the structure, properties and functions defined in the class statement.
From then on, you can access various properties or call functions within it.
class show_diary
{
public $owner;
public function __construct()
{
$this->owner='My';
}
}
class view_diary
{
public $owner;
public $foo;
public function __construct()
{
$this->foo='bar';
$this->owner=new show_diary();
}
}
$diary= new view_diary();
print_r($diary);
The code gives us two classes. One of the classes has an instance of the other class within it.
I have used constructors, which are a special type of function that is executed each time we create a new instance of a class - basically each time we declare a variable of that type, the __construct function is called.
When the $diary= new view_diary(); code is called, it creates an instance of the view_diary class, and in doing so, the first thing it does is assigns it's own foo property to have the value 'bar' in it. Then, it sets it's owner property to be an instance of show_diary which in turn then kicks off the __construct function within the new instance. That in turn assigns the owner property of the child item to have the value 'My'.
If you want to access single properties of the object, you can do so by the following syntax:
echo $diary->foo;
To access a property of an object inside the object, you simply add more arrows:
echo $diary->owner->owner;
Like this?
$diary = $this->show_diary . '_diary';
return $this->$diary();
I have declared a static method in class category
public static function getPrefixFromSubCategoyId($subCategoryId) {
$prefix = $this->fetch(array('table' => 'subCategories', 'id' => $subCategoryId));
return $prefix[0]['prefix'];
}
i am sure that i am using correct piece of code because when i use the same code outside the class scope with following code it works properly
$category = new Category($dbh);
$subCategoryId = 6;
$prefix = $category->fetch(array('table' => 'subCategories', 'id' => $subCategoryId));
echo $prefix[0]['prefix'];
but when i initialize the static method with following syntax.
$prefix = Category::getPrefixFromSubCategoyId(4);
it gives me following error.
Fatal error: Using $this when not in object context
am i missing something? or am i declaring it the wrong way?
thank you..
static methods are class members and aren't bound to an object. This means, that $this simply doesn't exists. You cannot use it in static methods. If fetch() is static too, call it static
self::fetch(/* arguments */);
If not either getPrefixFromSubCategoyId() should not be static too, fetch() should be static (see example above), or you need an object
$tmp = new self;
$tmp->fetch(/* arguments */);
$this is a reference to the current object. It is not the reference to the class. Since you are using it statically you have no object. You would have to make a static call in there as well.
$this is used to get instance variables or methods (simple members and basically the current object if you have one defining with new) but when you want to reach the static variables you should use $self::some_varible and :: is scope resolution operator.
You must declare your methods or variables static if you do want to use them under a static function.