I dont know what we call this, but it will be something like this
$args = 1
function rendercomments($args,$usersholder = NULL ){
$args = $args + 1;
// missing
return true;
}
changeargs($args);
$userholder = 2
Can we do something like that?
Someway to make $args append to $usersholder without returning it?
Yes, all you need is to define your arguments "by reference" by using the ampersand (&).
function rendercomments(&$args, $usersholder = NULL){
$args += 1;
return true;
}
$args = 1;
rendercomments($args);
echo $args; // 2
I believe what you are asking for is to update the $userholder variable while inside the called function.
If so, you have two options:
Create a class that contains the $userholder and the changeargs($args); should be inside that class and you can just use the $this keyword to apply any changes to the $userholder object and the changes will continue outside of the changeargs() function.
Pass into the changeargs() function a reference to the $userholder variable so that you are modifying that variable the whole time.
Related
class UpcomingEvents {
//Variable I'm trying to make accessible and modify throughout the class methods
private static $postObjArr = array();
private static $postIdArr = array();
private static $pinnedPost;
//My attempt at a get method to solve this issue, it did not
private static function getPostObjArr() {
$postObjArr = static::$postObjArr;
return $postObjArr;
}
private static function sortByDateProp($a, $b) {
$Adate = strtotime(get_field('event_date',$a->ID));
$Bdate = strtotime(get_field('event_date',$b->ID));
if ($Adate == $Bdate) {
return 0;
}
return ($Adate < $Bdate) ? -1 : 1;
}
private static function queryDatesAndSort($args) {
$postQuery = new WP_Query( $args );
if( $postQuery->have_posts() ) {
while( $postQuery->have_posts() ) {
$postQuery->the_post();
//Trying to push to the array, to no avail
array_push(static::getPostObjArr(), get_post());
}
}
//Trying to return the array after pushing to it, comes back empty
return(var_dump(static::getPostObjArr()));
//Trying to sort it
usort(static::getPostObjArr(), array(self,'sortByDateProp'));
foreach (static::getPostObjArr() as $key => $value) {
array_push(static::$postIdArr, $value->ID);
}
}
}
I'm trying to access $postObjArr within the class, and push to it with the queryDatesAndSort(); method. I've tried a couple of things, most recent being to use a get method for the variable. I don't want to make it global as it's bad practice I've heard. I've also tried passing by reference I.E
&static::$postObjArr;
But when it hits the vardump, it spits out an empty array. What would be the solution and best practice here? To allow the class methods to access and modify a single static array variable.
static::$postObjArr[] = get_post()
I didn't think it would of made a difference, but it worked. Can you explain to me why that worked but array.push(); Did not?
Arrays are always copy-on-write in PHP. If you assign an array to another variable, pass it into a function, or return it from a function, it's for all intents and purposes a different, new array. Modifying it does not modify the "original" array. If you want to pass an array around and continue to modify the original array, you'll have to use pass-by-reference everywhere. Meaning you will have to add a & everywhere you assign it to a different variable, pass it into a function, or return it from a function. If you forget your & anywhere, the reference is broken.
Since that's rather annoying to work with, you rarely use references in PHP and you either modify your arrays directly (static::$postObjArr), or you use objects (stdClass or a custom class) instead which can be passed around without breaking reference.
I have a following php function:
public function getTrailer()
{
return $this->model->trailer;
}
How to assigned value of $this->model->trailer to $new variable. And can you explain me what $this->model->trailer means, please ?
Thanks !
To assigned the value to a new variable do this:$newVar = $this->model->trailer;
The meaning of $this->model->trailer is that is taking the value that are inside of the structure. Is looking what are inside $this, and inside the model and inside of trailer and taking the value.
I have a PHP function that I need to create that executes PHP or HTML Code that is stored in a variable. That's one problem. Another problem is the fact that the function will be created multiple times and will need a unique name so I've named it after $title but I'm not sure if this will create the function from the value of $title or if it will create a function called $title or if it just won't work. This is my code:
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
Some Code Here...
function $title ($output)
{
//Some Code Here to execute $output...
}
So... That's my problem. I'm not sure what will execute the $output cleanly so it can be shown on a page. And I don't know if the function will be named after the value of $title
Thanks for any help!
Ethan Brouwer
You can use call_user_func like this:
call_user_func($title, $output);
But it's really strange to change name of one function.
What you're trying to do is unfortunately not possible. You could create namespaces if you need a common name to prefix your functions with.
namespace fArr {
function func1 (args) {}
function func2 (args) {}
}
To call them you can use:
namespace {
fArr\func1($title, $output);
}
Generally you want to avoid creating totally anonymous functions. Maybe create a function that handles the title and output for all requests. Alternatively create a new object with the func name as a property and output assigned to it.
$title = $_POST['sogo_aso_name'];
$output = $_POST['sogo_aso_output'];
// Store data in an object named after the value of $title
// Note that constant use of this can get very messy, and very confusing
$$title = (object) array('output' => $output);
I wrote a function to display an object on each wordpress post:
function pod(){
global $post,$pod;
$id = get_the_ID();
$pod = new WP_POD( $id );
return $pod;
}
I thought $pod is global, so, I can use $pod->stuffs when need, but it doesn't work.
So, in each function I need to use stuffs in the object, I have to add one line:
$pod = pod()
I think repeatedly calling this function might not good for performence. Is there a way to make this global and accessable by other functions?
You want to avoid the global keyword. If you need to pull in data to a function, its a sure sign that your design is broken (and Wordpress is broken). Use Dependency Injection and pass in $post and $pod to the function (that will also prevent spooky action at a distance):
function pod($post, $pod)
{
$id = get_the_ID();
$pod = new WP_POD( $id );
return $pod;
}
However, that still doesnt make much sense. You are not using $post within the function, so why pass it in? Instead, you reach out of the function scope again to fetch some sort of id. And you use that to instantiate a WP_POD instance and assign it back to the global scope.
Why not just do
function createPod($id)
{
return new WP_POD($id);
}
and then call it with
$pod = createPod(get_the_ID());
or just delete the function altogether and just do
$pod = new WP_POD(get_the_ID());
Yes, that wont assign the $pod instance to the global scope, but I doubt that you really need it there anyways.
As for performance: you should not worry about performance unless you have profiled your application and found that it's running slow and that particular code is indeed the reason for it being slow.
You could simply pass the object to the functions you're calling.
Or use something else related to http://en.wikipedia.org/wiki/Dependency_injection
Or, if that doesn't convince you ;-), at least limit the visibilty of the variable via something like
function pod($init=false) {
global $post,$pod;
static $pod=null;
if ( is_null($pod) || $init ) {
$id = get_the_ID();
$pod = new WP_POD( $id );
}
return $pod;
}
see http://docs.php.net/language.oop5.static
You need to delcare $pod as global in the function you are trying to access the variable from. Variables are not accessible globally unless explicitly declared in PHP unlike most other languages.
Call pod() once
then access like this
global $pod;
$pod->stuffs ....
You can use $GLOBALS to declare the variable in your script:
$pod = new WP_POD( get_the_ID() );
function myFunc() {
$GLOBALS['pod']->aMethod;
}
The code
$global_obj = null;
class my_class
{
var $value;
function my_class()
{
global $global_obj;
$global_obj = &$this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
echoes 5, not 10.
"Upon first examination, it would seem that the constructor of my_class stores a reference to itself inside the $global_obj variable. Therefore, one would expect that, when we later change the value of $global_obj->my_value to 10, the corresponding value in $a would change as well. Unfortunately, the new operator does not return a reference, but a copy of the newly created object."
I still don't understand it, so can anyone please explain it differently, and help me understand?
Not sure why this is the way it works, but, if you remove the & in front of $this while assigning it to your global variable, it will work.
To illustrate that, the following portion of code :
$global_obj = null;
class my_class
{
public $my_value;
public function __construct()
{
global $global_obj;
$global_obj = $this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
Gives the following output :
10
Here are the differences with your code :
I remove the & before $this : with PHP 5, there is no need for that, when working with objects
I translated the code to real PHP 5 :
__construct for the constructor
use public/protected/private, and not var for properties
As a sidenote, the code you posted should have given you the following warning :
Strict standards: Creating default object from empty value
Notes :
I'm using PHP 5.3.2
E_ALL doesn't include E_STRICT (source)
EDIT after some more searching :
Going through the References Explained section of the PHP manual, and, more specifically the What References Do page, there is a warning given that says (quoting) :
If you assign a reference to a
variable declared global inside a
function, the reference will be
visible only inside the function. You
can avoid this by using the $GLOBALS
array.
And there is an example going with it.
Trying to use $GLOBALS in your code, I have this portion of code :
$global_obj = null;
class my_class
{
public $my_value;
public function __construct()
{
$GLOBALS['global_obj'] = & $this;
}
}
$a = new my_class;
$a->my_value = 5;
$global_obj->my_value = 10;
echo $a->my_value;
And I get the following output :
10
Which seems to work ;-)
If I replace the __construct method by this :
public function __construct()
{
global $global_obj;
$global_obj = & $this;
}
It doesn't work...
So it seems you should not use global, here, but $GLOBALS.
The explanation given in the manual is :
Think about global $var; as a
shortcut to $var =&
$GLOBALS['var'];. Thus assigning
another reference to $var only
changes the local variable's
reference.
And, just so it's said : using global variables is generally not quite a good idea -- and, in this specific situation, it feels like a very bad idea...
(Now, if this question what just to understand why... Well, I can understand your curiosity ;-) )