I've been looking over the PHP variable scope reference page and I can't seem to find a clear answer. I'm working with this basic model where I want to call a class function and have all of its variables accessible outside the class. I want to avoid using the global declaration as well and do this in the most efficient way possible.
class my_class() {
function my_class_function() {
$my_class_function_variable = 'juice';
return $my_class_function_variable;
}
}
$class_instance = new my_class();
function display_juice() {
$class_instance::my_class_function();
$my_class_function_variable;
}
Is there a quick answer in what I'm missing? I'm not getting any output or errors.
EDIT
I'm getting a syntax error when I declare the following within the class but not within the function.
public $current_user = wp_get_current_user();
Parse error: syntax error, unexpected '(', expecting ',' or ';'
You can access all setted variables in class if they are public.
And you can access any of its function if its again public...
Here is example . I have default value 'mydefault'.
I use it, then I change it and again I use it..
class my_class {
public $variable = "mydefault";
public function change_variable($value) {
$this->variable = $value;
}
}
function display_juice() {
$class = new my_class;
echo $class->variable; // echo default value - 'mydefault'
$class->change_variable('newvalue');
echo $class->variable; // echo default value - 'newvalue'
}
display_juice();
Edit..
class my_class {
public $variable;
public function __construct() {
$this->variable = wp_get_current_user();
}
}
You need to make my_class_function() static, the you can call it with my_class::my_class_function()
http://php.net/manual/en/language.oop5.static.php
Also as suggested bellow, you need to return 'juice'.
return 'juice';
You currently don't return anything, you would have to do something like this:
class my_class() {
function my_class_function() {
$my_class_function_variable = 'juice';
return $my_class_function_variable;
}
}
function display_juice() {
$class_instance = new my_class(); //Needs to be inside the function
$my_class_function_variable = $class_instance->my_class_function();
echo $my_class_function_variable;
}
display_juice();
If you want to use a static function or variable you could do this, which eliminates the need for having to create an instance of my_class:
class my_class() {
static function my_class_function() {
return 'juice';
}
}
function display_juice() {
$my_class_function_variable = my_class::my_class_function();
echo $my_class_function_variable;
}
display_juice();
I would make the variable you're trying to access a public member
class my_class {
public $my_class_function_variable = 'juice';
public function my_class_function() {
}
}
$class_instance = new my_class();
function display_juice() {
echo my_class->$my_class_function_variable;
}
display_juice();
Related
I'm trying to do something like this:
First, I create the identity class
class Identity {
private $identity;
public function __construct($identiy) {
$this->identity = $identiy;
}
public function getIdentity($value) {
return $this->identity($value);
}
}
Then I create an instance of the class:
$identity = new Identity(function ($value){
return "1";
});
echo $identity->identity(1);
In JavaScript, I can do something like that but I don't know how to do it in PHP. Any help is appreciated.
You have 2 problems:
Your property is private, so you need to use the getter to access it.
There is no identity method which will cause php to error out. Using a temporary variable solves that.
class Identity {
private $identity;
public function __construct($identiy) {
$this->identity = $identiy;
}
public function getIdentity($value) {
// Use a temporary variable for your function
$func = $this->identity;
return $func($value);
}
}
$identity = new Identity(function ($value){
return "1";
});
// Access the getter instead of the property
echo $identity->getIdentity(1);
An example.
Here is the documentation for what you need:
$var = function() {
return 'I am a ' . func_get_arg(0);
};
print_r($var('Closure'));
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();
I have the following setup:
class test {
public static function something() {
$somethingElseFunction = "somethingElse";
// How can I call the method with the name saved in variable?
}
public static function somethingElse($a) {
echo 'bla';
}
}
How can I call the function using the variable? (the function name is in variable).
Also I need to do a function_exists() for it.
Tried this:
if (function_exists(self::$somethingElseFunction ())) {
if (!call_user_func(self::$somethingElseFunction , $a)) {
}
}
Didn't work.
In PHP>=5.4 you can use just self:: de-reference:
self::$somethingElseFunction();
-but in earlier versions that will cause error (because it wasn't allowed to use dynamic static methods de-reference). So then you can always use such things as call_user_func_array() :
class test {
public static function something() {
$somethingElseFunction = "somethingElse";
call_user_func_array(array(__CLASS__, $somethingElseFunction), array("bla"));
}
public static function somethingElse($a) {
var_dump($a);
}
}
test::something();
-this will work for PHP>=5.0
About function_exists() call - it expects string as parameter, thus I recommend to use method_exists() - because that function is intended to do the stuff:
public static function something() {
$somethingElseFunction = "somethingElse";
if(method_exists(__CLASS__, $somethingElseFunction))
{
call_user_func_array(array(__CLASS__, $somethingElseFunction), array("bla"));
}
}
You should be able to use the following:
test::$somethingElseFunction();
Use this function:
$classname = 'somethingElse';
call_user_func('test::' . $classname, $params);
<?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 started learning oop php and I don't understand how to make a method inside a class execute.
This is the code:
class GrabData {
public $tables=array();
public $columns=array();
public $argList;
function __construct(){
$this->argList=func_get_args();
$pattern;
var_dump($this->argList);
if(!empty($this->argList)){
foreach($this->argList as $value){
if(preg_match("/.+_data/",$value,$matches)){
if(!in_array($matches[0],$this->tables)){
array_push($this->tables,$matches[0]);
var_dump($this->tables);
}
$pattern="/". $matches[0] . "_" . "/";
array_push($this->columns,preg_replace($pattern,"",$value));
var_dump($this->columns);
}
}
}
}
public function gen_query(){
var_dump($this->argList);
echo "haha";
}
gen_query();
}
new GrabData("apt_data_aptname");
Now, the __construct function runs when I make a new GrabData object, but the gen_query function doesnt execute. How do I make it execute it ?
If you always want to have the gen_query function run when the class is initiated, you could link to it in the bottom of your constructor, like so:
function __construct() {
// Do your stuff here and then...
$this->gen_query();
}
First, you assign the object returned by the new operator to a variable - then use that variable to execute methods on your object:
class GrabData {
public $tables=array();
public $columns=array();
public $argList;
function __construct() {
$this->argList=func_get_args();
$pattern;
var_dump($this->argList);
if(!empty($this->argList)){
foreach($this->argList as $value){
if(preg_match("/.+_data/",$value,$matches)){
if(!in_array($matches[0],$this->tables)){
array_push($this->tables,$matches[0]);
var_dump($this->tables);
}
$pattern="/". $matches[0] . "_" . "/";
array_push($this->columns,preg_replace($pattern,"",$value));
var_dump($this->columns);
}
}
}
}
public function gen_query() {
var_dump($this->argList);
echo "haha";
}
}
$super_object = new GrabData("apt_data_aptname");
$super_object->gen_query();
A different way to run a class function without initiating the class is to use the double colon scope resolution operator, strangely called "Paamayim Nekudotayim".
GrabData::gen_query();
You can read up on it here.