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();
Related
I have a function and I'd like to return a variable to another function.
Can I return the array variable so I can use the variable at other function?
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
When I try to use $araydatamdr in another function, it became 0.
Am I missing something?
You can achieve this by calling function and setting its return value to another variable.
Method 1 :
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function update_mdr_pameran() {
//global $araydatamdr;
$this->config->set_item('compress_output', FALSE);
$araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
return $araydatamdr;
}
public function test_func() {
$araydatamdr = $this->update_mdr_pameran();
var_dump($araydatamdr);
}
}
Or you can also set $araydatamdr to $this reference.
Method 2 :
class Test extends CI_Controller {
public $araydatamdr;
public function __construct() {
parent::__construct();
$this->araydatamdr = [];
}
public function update_mdr_pameran() {
$this->config->set_item('compress_output', FALSE);
$this->araydatamdr['mdr_debit'] = trim($this->input->post('mdr_debit'));
$this->araydatamdr['mdr_debit_npg'] = trim($this->input->post('mdr_debit_npg'));
$this->araydatamdr['mdr_debit_pl'] = trim($this->input->post('mdr_debit_pl'));
}
public function test_func() {
$this->update_mdr_pameran();
var_dump($this->araydatamdr);
}
}
Cross out the echo $araydatamdr; Arrays can be printed using var_dump or print_r. Also you can return an array in php but personally i prefer to json_encode it first so i return a json as the output of my function something like:
return json_encode($araydatamdr);
Then it's a simple function call.
I don't know your project structure but i am giving general guidance. Apart from that i don't see anything else that could block your function.
I edit my post because i saw the issue is to call the function. There are 2 ways depending where you call it. If the function is in the same class as the other function you want to call it you simple go for :
$result=$this->update_mdr_pameran();
I see that your function has no arguments so you don't need to set any. If it's in another file:
1) include your php file at top like :
require 'myphpclass.php';
*tip make sure your path is right.
2) Create a new class object and then call the function like :
$class= new myClass();
$result=$class->update_mdr_pameran();
We can change the static variable value of a class from outside, thats the advantage of static variables but how do we change static method from outside ?
<?php
class A
{
static $static_var = 0;
static function test(){
return 'i want to change inside this test method';
}
}
echo A::$static_var; // outputs 0
++A::$static_var;
echo A::$static_var; // ouputs 1
// Now how do we do something to change the static test method body? is it possible ?
like
A::test() = function(){ /* this is wrong */}
}
As #Mark Baker said, you can only change variable...
But there is a way to declare variable as callable, you can use anonymous function.
here is the documentation: http://php.net/manual/en/functions.anonymous.php
class A
{
public static $method;
}
A::$method = function() {
echo 'A';
};
call_user_func(A::$method);
// OR
$method = A::$method;
$method();
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();
?>
<?php
try{
$test = new TestAccessModifiers("2345","xyz","vfd","a0001","99","67"); /*invoking the class*/
var_dump($test->calculate());
}
catch(Exception $e){
echo $e->getMessage();
}
?>
<?php
class TestAccessModifiers {
function TestAccessModifiers($user_p,$user_fn,$user_ln,$user_id,$marks1,$marks2) {
echo "hello1";
$this->user_phone=$user_p;
$this->user_fname=$user_fn;
$this->user_lname=$user_ln;
$this->user_id=$user_id;
$this->marks1=$marks1;
$this->marks2=$marks2;
echo $this->marks1;
}
private $additional_marks = 10;
public static function calculate(){
return $this->marks1+$this->marks2+$this->getAdditionalmarks();
}
public function getAdditionalmarks(){
return $this->additional_marks;
}
}
?>
Above is the simple code i am trying to run... but i am unable to call TestAccessModifiers
I have tried using _constructor too
Rename your TestAccessModifiers function to __construct.
public function __construct($user_p,$user_fn,$user_ln,$user_id,$marks1,$marks2) {
echo "hello1";
$this->user_phone = $user_p;
$this->user_fname = $user_fn;
$this->user_lname = $user_ln;
$this->user_id = $user_id;
$this->marks1 = $marks1;
$this->marks2 = $marks2;
echo $this->marks1;
}
Then, remove static from calculate function.
It should then works..
Reference: http://php.net/manual/en/language.oop5.decon.php
if you are calling the class in another php page, make sure you include it like this.
include('/path/to/your/class.php');
$test = new TestAccessModifiers("2345", "xyz", "vfd", "a0001", "99", "67");
or if you are instantiating the object within the same file then place the instantiation code below your class.
class TestAccessModifiers {
public function __construct($user_p, $user_fn, $user_ln, $user_id, $marks1, $marks2) {
echo "hello1";
$this->user_phone = $user_p;
$this->user_fname = $user_fn;
$this->user_lname = $user_ln;
$this->user_id = $user_id;
$this->marks1 = $marks1;
$this->marks2 = $marks2;
echo $this->marks1;
}
private $additional_marks = 10;
public function calculate() {
return $this->marks1 + $this->marks2 + $this->getAdditionalmarks();
}
public function getAdditionalmarks() {
return $this->additional_marks;
}
}
try {
$test = new TestAccessModifiers("2345", "xyz", "vfd", "a0001", "99", "67"); /*invoking the class*/
var_dump($test->calculate());
} catch (Exception $e) {
echo $e->getMessage();
}
you have defined a static method in your class, and have used the pseudo variable $this inside of the static method, which PHP does not allow. since static method is treated out of object context in PHP. you need to remove the static method to use $this
First: Which PHP version you have?
Second: Are you including the class file in the file that you are instancing?
Third: Your function "calculate" is static, then, you can't access to it from an instance and it have no way to read or write properties non-statics.
Try TestAccessModifiers::calculate(); and put in a simple return "Hello World"
Greetings.
What version of PHP are we talking about?
First thing comes to mind: did you add it to autoload ?
I would use the __constructor methode as initiator routine of the class.
public static function calculate() {
return $this - > marks1 + $this - > marks2 + $this - > getAdditionalmarks();
}
You can't operate on $this from a static context. Make this method non-static, or adjust the other variables to be static, whichever suits your context.
I have a similar code snippet like this
class Search
{
public function search($for, $regEx, $flag) //I would like this to be the constructor
{
// logic here
return $this;
}
}
Then I have another class that creates an object from it, later than tries to use the object.
class MyClass
{
public function start()
{
$this->search = new Search();
}
public function load()
{
$this->search($for, $regEx, $flag);
}
}
My question is, is it possible to create an object first THEN give it the parameters?
I know there are some way around this BUT I only ask because I want to use the object like this
$this->search($params);
// I have my methods chained, so I could use it in one line like
// $this->search($params)->hasResults();
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
The way I have it set up right now, I would need to use it like this
$this->search->search($params);
if ($this->search->hasResults()) {
echo 'found stuff';
} else {
echo 'didn't find anything';
}
I have a method called search() that does the logic, and I don't want to be redundant in my naming nor do I want to change the name of the method.
I know another way to keep the visual appeal sane I could pass a variable like so
$search = $this->search->search($params);
then
$search->hasResults();
At the same time I am trying to introduce myself to new OOP concepts and learn from them. Would this require passing things by reference? or setting up some type of magic method?
While the previous anwsers show that you can, I wouldn't use it, because it breaks the concept of encapsulation. A proper way to achieve what you want is the following
class Search
{
public function __constructor($for='', $regEx='', $flag='')
{
$this->Setup($for, $regEx, $flag);
}
public function Setup($for, $regEx, $flag)
{
//assign params
//clear last result search
//chain
return $this;
}
public function search()
{
// logic here
return $this;
}
}
In this way, you can reuse the object and have the params in the constructor, without breaking encapsulation.
Yes it is possible
See the below example
<?php
class a{
public $a = 5;
public function __construct($var){
$this->a = $var;
}
}
$delta = new a(10);
echo $delta->a."\n";
$delta->__construct(15);
echo $delta->a."\n";
Output will be:
10 15
Yep, you can.
class Example {
public $any;
function __counstruct($parameters,$some_text) {
$this->any=$some_text;
return $this->any;
}
}
You can call constructor:
$obj = new Example (true,'hello');
echo $obj->any;
$obj->__construct(true,'bye-bye');
echo $obj->any;
I was able to create the visual coding I wanted by using the __call() magic method like this
public function __call($name, $params)
{
$call = ucfirst($name);
$this->$name = new $call($params);
}
from there I could use this
$this->test->search($params);
$this->test->search->hasResults();
I of course now set the search() method to the class constructor