Make a variable accessible in all test methods. PHPUnit - php

In my test class I want a variable to be accessible from any test method. I know this is possible by initializing it in setUp method or declaring in bootstrap file.
But the issue is that the value of variable is unknown until test starts. It gets generated during a test and then used by subsequent test methods.
Currently I use this value by declaring subsequent methods to depend on the method, which generates the value, and then passing the value using return statement from method generating the value. But I don't think this is proper way as I have to add return statement just to make variable accessible elsewhere.
Is there any standard way to make variable accessible to every method which gets generated dynamically during test method execution?

Follow these steps:
1. Create a public class.
2. Declare the variable as global.
You can reuse the variable in any part of your application.

Related

Static variable in php without objects

I want to define a php variable that can be accessed in all the scripts and is initialized only one time per runtime. This is, like a static variable but with no object. I thought of using $GLOBALS but noticed the variables do not stick between script executions and I would have to instantiate the variable everytime. So how exactly can I accomplish this? Would I need to create an abstract class only to static the variable?
how about using $_SESSION? see http://www.w3schools.com/php/php_sessions.asp

PHP classes - is it preferable to use normal variables instead of properties?

For my latest website I’ve been trying to use classes. Mainly to teach myself more about OOP and learn through experience.
Whenever I needed a ‘variable’ within my class I created a property, for instance:
class someClass
{
var $valueToUseHere; // Only used internally - can I just use a variable?
public function doStuff()
{
$this->valueToUseHere = 60;
// Do more stuff here...
}
}
It is only now when I’ve been looking more into the code and trying to do some optimisation that I’ve noticed that my functions and classes are passing around some large objects. A lot of that bulk could be stripped away if I made all the properties that are only used inside the class into normal variables.
Are properties only used for variables that are needed outside the class and is it then acceptable to just use ‘normal’ variables within the class itself ?
Sorry if this question illustrates a lack of understanding on the subject. Unfortunately, this is where my learning is up to at this point. I’ve done some searching around “class properties vs variables” etc but not found a comprehensive answer to this.
Many thanks
It's somewhat vague what you're asking, but if valueToUseHere is not used outside of doStuff, then don't make it a property!
class someClass {
public function doStuff() {
$valueToUseHere = 60;
// Do more stuff here...
}
}
If there's no reason to share that value with other methods of the class or with the outside world, then there's no reason to clutter up your object with all sorts of properties. Not only may this cause tricky bugs with preserved state, it also forces you to be unnecessarily careful with your variable names across all object methods.
Actually class properties are variables as well. Basically you have three options:
Global variable, available everywhere, but not recommended because all parts of your code may depend on such a varialbe, changes can easily break stuff everywhere.
Class property (Note: you should define a visibility - public/protected/private) these properties are bound to the object instance and should be used for any state that the object needs to keep for further processing. Usually those might be used in more than one metohd of your class.
Variables inside a method like just
public function doStuff()
{
$valueToUseHere = 60;
// Do more stuff here...
}
The variable is just available inside the method and is thrown away at the end of the method execution.
That depends on your needs. If you are going to simply hold a value in a variable it's the best to keep it's simplicity and not define functions for setting or getting it's value. But sometimes you may need to have more controls on a variable in your class. For example you have defined an integer variable and you want it's values to be always between 10 and 1000 and also it should not be in 100,200,300,..,900. So here there is a good reason to set your variable access to private and create a public function to check what is required before setting a new value. Or in another example you may want to call another function or change another depended variable in your class exactly after this variable changed. Or if you want to make a variable read-only or write-only always you can define properties for controlling the variable value.
In brief you may prefer to use:
Properties: When you want to have control about get and set values
Variables: When you want to set or use a variable as its nature

How come it is possible to access variables without $this in the view?

I started working with Zend Framework 2 a short while ago.
In the controller, I am sending variables to view using
return $viewmodel->setVariables(array(
'exampleVariable' => 'exampleValue',
'exampleVariable2' => array(
'variableInArray' => $this->getMacAddress(),
),
));
In the view I was doing:
$exampleVariable = $this->exampleVariable
// and
$exampleVariable2 = $this->exampleVariable2
and then using those variable directly so I don't have to go through $this each time I use them.
I was working on it, and was modified a few stuff, and while I wanted to debug, I removed those two previous lines hoping it would break.
To my surprise, the $exampleVariable and $exampleVariable2 were still available. At first, I thought it was a caching problem, but it turns out all the array keys that are sent to the view with SetVariables() can be accessed as variables.
My question is, how come it is possible to access them without $this?
I am probably gonna be warned about this, but this question if just for curiosity. I won't use the variables directly, as I prefer to create them in the view so I can comment them and add their respective variable types and stuff.
Take a look here
Variables assigned to the view – either via a View Model, Variables container, or simply by passing an array of variables to render()– may be retrieved in three ways:
Explicitly, by retrieving them from the Variables container composed in the PhpRenderer: $this->vars()->varname.
As instance properties of the PhpRenderer instance: $this->varname. (In this situation, instance property access is simply proxying to the composed Variables instance.)
As local PHP variables: $varname. The PhpRenderer extracts the members of the Variables container locally.
The explanation:
The PhpRenderer uses extract function to extract the variables into the function (render) scope. This allows to use $exampleVariable in the template. Further the PhpRenderer uses the magic __get function. So if you call $this->exampleVariable it looks in the data array directly.
If you take a look at the PHPRenderer class, specifically PHPRendered::render()
Zend\View\Renderer\PHPRenderer
You will see how the Views are generated, using extract (http://php.net/extract)
This allows any of the views variables to be accessed locally inside the view / template.

PHP object scope

Can somebody explain to me the concept of object scope in PHP? I'm very new to objects in PHP and the reason I ask is because I was able to create an object within an if statement, and then access the object outside the scope of the if statement.
Example:
//only create object if some condition is met
if ($conditionTrue){
$myBook = new Book('PHP for Dummies','softcopy');
}
$myBook.read();
I would have though that this would generate an error but it didn't.
Some background to my question
I was trying to figure out how to determine which constructor to call depending on the condition that is met. The only conceivable way was to introduce an if statement but doing that, I thought would impose the issue of scope which it didn't and I'm just wondering why..
In PHP, if doesn't have its own scope. So yes, if you define something inside the if statement or inside the block, then it will be available just as if you defined it outside (assuming, of course, the code inside the block or inside the if statement gets to run).For more about the PHP scope, read the variable scope manual page.
This scenario will generate error in other languages like JAVA,C#. But in PHP this is not going to happen.
Because in PHP we can create variable anywhere there is no need to first initialise the variable and after that assign the values in it.
In this scenario when you assigns the value to $myBook it first initialises the variable $myBook for global scope. So when you accessing the $myBook outside if block, it is already present in global scope of document and because of that you can access it without generating eror.
The above scenario has some limitation like where the variable is initialised, eg (within function, within class).

Codeigniter : variables scope when calling a view from within a view. Odd

I am confused about variables scope when calling a view from within a view.
I tested a bit and found:
If the variables are originally passed from a controller, there is no need to do something to pass the variables from a view to a view.
If the variables are declared in a view, I have to explicitly pass the variables from a view to a view. (e.g. : $this->load->view("hoge", $data);)
I find it bit odd about the second case because my understanding was $this->load->view() is codeigniter version of the php include() which doesn't require me to pass variables explicitly.
Can anyone guess/shed a little light on why they did this?
If you look at the Loader library's _ci_load method (which view() calls), around line 639 in the latest version, you'll see this bit of code:
/*
* Extract and cache variables
*
* You can either set variables using the dedicated $this->load_vars()
* function or via the second parameter of this function. We'll merge
* the two types and cache them so that views that are embedded within
* other views can have access to these variables.
*/
if (is_array($_ci_vars))
{
$this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars);
}
extract($this->_ci_cached_vars);
That's why your variables passed to the view are available automatically in nested views.
But your locally declared variable aren't.. because they aren't passed on to the next view() call.
Note that it merges the variables, so technically, you could just pass to your subview the variable that are changed in the top view, the rest will be inherited automatically.
IMHO though, I think that for the sake of clarity and other potential people reading your code it's best to always pass on explicitly all the variables that your subview will require.. code becomes easier to read/debug.
Note: a side effect of the caching is that if you have 2 subviews, variables passed to the first one will get cached and get automatically passed on to the second view as well.. that can lead to debugging trouble sometimes.

Categories