PHP - class forget set variables - php

I try to create some sort of setup class, like global values for the page.
The PHP-code
class globals
{
public $page;
public function __construct()
{
}
public function set_page($value)
{
$this->page = $value; // Maybe from a database
}
}
class get
{
public function page()
{
$globals = new globals();
return $globals->page;
}
}
$globals = new globals();
$globals->set_page('My value');
echo get::page(); // Short function to be in a template
Question
My class forget the value I set. Why is that?
Do I have to use global variables?
Is this the correct approach for the problem?

The variable is set on an object, not on a class.
For each class, you can instantiate multiple objects. Each of those have their own variable scope.

Edit:
I forgot to include the easiest, and least verbose solution to your problem. AFAIK, you're looking for a way to check what page you're on. Constants will do just that:
defined('MY_CURRENT_PAGE') || define('MY_CURRENT_PAGE','My Value');
//use anywhere like so:
echo 'Currently on page: '.MY_CURRENT_PAGE;
My class forget the value I set. Why is that?
Quite simple: your page member function isn't static, yet you call it as though it is: get::page(). Even if you were to fix this, you're creating a new instance in the page method, but you're not preserving a reference too it anywhere, so each page call will create a new globals instance, that has nothing set.
Do I have to use global variables?
No, unless you're Really desperate, never use globals
Is this the correct approach for the problem?
No, if it doesn't work, it's not correct (IMHO).
Well, what is, you might ask. There are several ways to go about this:
class globals
{
public static $page = null;//make this static, meaning all instances will share this var
public function set_page($value)
{
self::$page = $value; // Maybe from a database
}
}
class get
{
private $_globalsInstance = null;
public function __construct(globals $instance = null)
{
$this->_globalsInstance = $instance;
}
private function _getGlobals()
{
if (!$this->_globalsInstance instanceof globals)
{
$this->_globalsInstance = new globals();
}
return $this->_globalsInstance;
}
public function page()
{
return $this->_getGlobals()::$page;
}
}
Personally, however, I wouldn't work like this, I'd just pass my instances to wherever I need them (as arguments to functions/methods or just instantiate them in a scope that will be accessible:
class globals
{
public $page = null;//make this static, meaning all instances will share this var
public function set_page($value)
{
$this->page = $value; // Maybe from a database
}
}
$page = new globals();
$page->set_page('foobar');
someFunction($page);
$someObject->renderPage($page);
require_once('specificScript.php');
//inside required script:
echo $page->page;

Do I have to use global variables?
Not, if your can use PHP 5.3
Is this the correct approach for the problem?
Better to use a generic class for this, or use static properties of objects
<?php
class globals
{
public static $page;
public function __construct()
{
}
public function set_page($value)
{
self::$page = $value; // Maybe from a database
}
}
class get
{
public static function page()
{
return globals::$page;
}
}
$globals = new globals();
$globals->set_page('My value');
echo get::page(); // Short function to be in a template
P.S.
But this is not a nice approach

$globals there
class get
{
public function page()
{
$globals = new globals();
return $globals->page;
}
}
and there
$globals = new globals();
$globals->set_page('My value');
are different inctances of globals class.
One of the solutions is to make $page var static
public static $page;
I hope this helps
UPD:
Also you might apply Singleton to globals class and request for its insnance instead of creating new one directly:
globals::getInstance()->setPage('Page');
and
return globals::getInstance()->getPage();
In this case $page doesn't have to be static.

I'm not sure the other answers are very clear. You have created 2 classes. As such they have different scopes. As writen you can't access the original variable $page from the get class because it's outside the scope. Your page function in fact creates a new version of the object $globals without $page set. Normally you would place both your set and get functions in the initial object/class. Though it would be possible to use two class by calling the first class from the second and setting the page. Why you would want to do that I'm not sure.
if I were writing the class it would look like this.
class globals
{
public $page;
public function __construct()
{
}
public function set_page($value)
{
$this->page = $value; // Maybe from a database
}
public function get_page()
{
return $this->page;
}
}
Actually I would probably set page to private not public. As public I guess you don't need a get function.

for using methods of the class without object you must use static definition. but anyway you put value for one class object and try to get it from another...

Perhaps this will help you continue on your coarse:
class globals
{
public static $page;
public function set_page($value)
{
self::$page = $value; // Maybe from a database
}
}
class get extends globals
{
public function page()
{
$globals = new globals();
return parent::$page;
}
}
$globals = new globals();
$globals->set_page('My value');
echo get::page();
?>

Related

global variable can't accessible inside the function

I need to access the global variable from another function. First I have assigned the value to global variable in one function. When I am trying to get that value from another function, it always returns null. Here my code is
StockList.php
<?php
$_current;
class StockList
{
public function report(){
global $_current;
$_current = 10;
}
public function getValue(){
print_r($GLOBALS['_current']);
}
}
?>
Suggestion.php
<?php
include ("StockList.php");
$stk = new StockList();
$stk->getValue();
?>
Thanks in advance.
Man, its hard to understand what are you trying to do as you said you have called report() in your index.php
Anyways, when dealing with classes, to set variable values, standard procedure is as following:
class StockList
{
public $_current;
public function setValue($value){
$this->current = $value;
}
public function getValue(){
return $this->current;
}
}
And after whenever you wanna use the class:
<?php
include ("StockList.php");
$stk = new StockList();
$stk->setValue(10);
$_current = $stk->getValue();
var_dump($_current);
?>
This is basic idea of OOP, benefits of this approach are:
You can dynamically set value of $_current.
Your getValue() function is not dedicated for printing the value of the variable, thats why you can use that function only for getting the value and then do whatever you want with it.

PHP If Statement in Class to set protected variable

I'm trying to set a class Variable dependent on an if statement in PHP that's available to all the functions in the class. Just setting the variable works fine, but as soon as I attempt to set from an IF statement everything is breaking.
Examples:
Works
class Default_Service_NewSugar {
protected $base_url = "http://url1";
function test() {
return $this->base_url;
}
}
Doesn't work
class Default_Service_NewSugar {
if($_SERVER['APPLICATION_ENV']=="development"){
protected $base_url = "http://url1";
} else {
protected $base_url = "https://url2";
}
function test() {
return $this->base_url;
}
}
Is this not possible in a class? If not is there an alternative way I should be approaching this?
The main problem is that you are putting procedural code inside a class. Referencing to the PHP.net documentation:
A class may contain its own constants, variables (called "properties"), and functions (called "methods").
php.net
I would recommend reading the PHP manual on how to work with OOP, and read many of the OOP tutorials available on the web.
As mentioned in other answers you should do the initializing work inside the class constructor.
class Default_Service_NewSugar
{
protected $base_url;
public function __construct()
{
$this->base_url = ($_SERVER['APPLICATION_ENV'] == "development")
? "http://url1"
: "https://url2";
}
function test()
{
return $this->base_url;
}
}
A more OOP like approach would be to set the URL inside a configuration file and pass the variable to the class when initiating the class.
class Default_Service_NewSugar
{
protected $base_url;
public function __construct($base_url)
{
$this->base_url = $base_url;
}
function test()
{
return $this->base_url;
}
}
//usage would then be:
$default_service = new Default_Service_NewSugar($url_from_configuration_file);
$default_service->test(); //outputs the given URL
You should write that conditional statement inside the constructor. And also, no need to specify the access specifier every time the variable is being initialized, you can specify once and in the initialization part just assign the value. And also optimize your code as well.
class Default_Service_NewSugar {
protected $base_url = "http://url";
function __construct() {
$this->base_url .= $_SERVER['APPLICATION_ENV']=="development" ? 1 : 2;
}
function test() {
return $this->base_url;
}
}
You have lots of typos and that's not the perfect way to work around variables within class
class Default_Service_NewSugar {
protected $base_url;
public function __construct() {
$this->base_url = ($_SERVER['APPLICATION_ENV'] == "development") ? "http://url1" : "http://url2";
}
public function test() {
return $this->base_url;
}
}
As already mentioned, you may initiate your variable inside constructor. Another approach to achieve your goal is to use property access function instead of direct object field access. In this way, you may add additional conditions easily and the code will look like this:
class Default_Service_NewSugar {
public function __construct() {
}
public function getBaseURL($env = null) {
$env = $env ? $_SERVER['APPLICATION_ENV'] : $env;
return ($env == "development") ? "http://url1" : "http://url2";
}
}
$obj = new Default_Service_NewSugar();
print $obj->getBaseURL();

Execute a method when an instance variable is referenced in a PHP class?

Is it possible to run a function when referring to a variable in a php class rather than simply returning its value, similar to javascript's ability for a variable to hold a method?
class LazyClassTest()
{
protected $_lazyInitializedVar;
public function __construct()
{
/* // How can this call and return runWhenReferrenced() when
// someone refers to it outside of the class:
$class = new LazyClass();
$class->lazy;
// Such that $class->lazy calls $this->runWhenReferrenced each
// time it is referred to via $class->lazy?
*/
$this->lazy = $this->runWhenReferrenced();
}
protected function runWhenReferrenced()
{
if (!$this->_lazyInitializedVar) {
$this->_lazyInitializedVar = 'someValue';
}
return $this->_lazyInitializedVar
}
}
PHP5s magic method __get($key) and __set($key, $value) might be what you need. More information about them is available in the PHP manual.
This sounds like PHP5.3: lambda / closures / anonymous functions
http://php.net/manual/en/functions.anonymous.php:
<?php
$greet = function($name) {
printf("Hello %s\r\n", $name);
};
$greet('World');
$greet('PHP');
?>
You are probably heading in the wrong direction. You normally want to define a getter getLazyVar(). There is a reason why people always make properties protected and defined getters / setters: So they can pre- or postprocess the values.

Assigning a function's result to a variable within a PHP class? OOP Weirdness

I know you can assign a function's return value to a variable and use it, like this:
function standardModel()
{
return "Higgs Boson";
}
$nextBigThing = standardModel();
echo $nextBigThing;
So someone please tell me why the following doesn't work? Or is it just not implemented yet? Am I missing something?
class standardModel
{
private function nextBigThing()
{
return "Higgs Boson";
}
public $nextBigThing = $this->nextBigThing();
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing; // get var, not the function directly
I know I could do this:
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel;
echo $standardModel->nextBigThing(); // Call to the function itself
But in my project's case, all of the information stored in the class are predefined public vars, except one of them, which needs to compute the value at runtime.
I want it consistent so I nor any other developer using this project has to remember that one value has to be function call rather then a var call.
But don't worry about my project, I'm mainly just wondering why the inconsistency within PHP's interpreter?
Obviously, the examples are made up to simplify things. Please don't question "why" I need to put said function in the class. I don't need a lesson on proper OOP and this is just a proof of concept. Thanks!
public $nextBigThing = $this->nextBigThing();
You can only initialize class members with constant values. I.e. you can't use functions or any sort of expression at this point. Furthermore, the class isn't even fully loaded at this point, so even if it was allowed you probably couldn't call its own functions on itself while it's still being constructed.
Do this:
class standardModel {
public $nextBigThing = null;
public function __construct() {
$this->nextBigThing = $this->nextBigThing();
}
private function nextBigThing() {
return "Higgs Boson";
}
}
You can't assign default values to properties like that unless that value is of a constant data type (such as string, int...etc). Anything that essentially processes code (such as a function, even $_SESSION values) can't be assigned as a default value to a property. What you can do though is assign the property whatever value you want inside of a constructor.
class test {
private $test_priv_prop;
public function __construct(){
$this->test_priv_prop = $this->test_method();
}
public function test_method(){
return "some value";
}
}
class standardModel
{
// Public instead of private
public function nextBigThing()
{
return "Higgs Boson";
}
}
$standardModel = new standardModel(); // corection
echo $standardModel->nextBigThing();

Dynamically picking variable from a class

I'm trying to get data from a class in php5, where the data in the class is private and the calling function is requesting a piece of data from the class. I want to be able to gain that specific piece of data from the private variables without using a case statement.
I want to do something to the effect of:
public function get_data($field)
{
return $this->(variable with name passed in $field, i.e. name);
}
You could just use
class Muffin
{
private $_colour = 'red';
public function get_data($field)
{
return $this->$field;
}
}
Then you could do:
$a = new Muffin();
var_dump($a->get_data('_colour'));
<?php
public function get_data($field)
{
return $this->{$field};
}
?>
You may want to look at the magical __get() function too, e.g.:
<?php
class Foo
{
private $prop = 'bar';
public function __get($key)
{
return $this->{$key};
}
}
$foo = new Foo();
echo $foo->prop;
?>
I would be careful with this kind of code, as it may allow too much of the class's internal data to be exposed.

Categories