Which approach is considered best practice?
Declare and check null...
class Foo {
$bar = null;
function test() {
return $this->bar != null;
}
}
Or, isset(...)...
class Foo {
function test() {
return isset($this->bar);
}
}
It makes a lot more sense to declare all the properties your class has, and check what their values are. It gives you a clear sense of what the class can do and what values it may have, rather than guessing every step of the way what property some other function may be looking for sometime later.
class Foo {
protected $bar = null;
public function test() {
return $this->bar !== null;
}
}
Note the !== check here; if you'd just use != the condition would be true for a number of other values besides just null.
It really is the exact same thing as any other variables in your code: you should always initialise any and all variables your code will work with, and then just worry about their values. isset should be used exclusively for cases where you have no control over the existence of a variable, e.g. as is the case for user input. For any other situation where you have perfect control over the existence or non-existence of your variable, opt for explicitly initialising them.
Related
I have a loop which will make calls to the function. Variables are defined (and reassigned on each iteration) in the first loop which are required for the function to function.
Loop:
if ($something) {
while (!$recordSet->EOF) {
$variable1 = TRUE;
$variable2 = FALSE;
...
function1()
}
}
Function:
function function1() {
if ($variable1 && !$variable2) {
...
}
}
The variables will have boolean values, and the environment is limited to PHP 4.
I'm currently considering using global $variable1; in the while loop and function1, but I know globals are almost always frowned upon.
Usually I'd use define("variable1","a value"), but the values will be changed multiple times.
Any suggestions, or is global defining the best solution in this case?
Thanks.
EDIT: Totally forgot to mention. This file is actually a spaghetti legacy code, and function1 is called in a hundred different places, all with varying bits of information. Otherwise, I would have used arguments.
In the main scope, define global $variable1 and global $varible2.
Also do it in the function.
But this is the worst solution. You will confuse, if these variables changes somewhere else.
The best way I think is to refactore your code, and pass varables as parameters.
The other solution could be create a class for these 2 variables, and set/get them statically.
class variablesPlaceHolder {
private static $variable1;
private static $variable2;
public static function getVariable1() {
return self::$variable1;
}
public static function getVariable2() {
return self::$variable2;
}
public static function setVariable1($variable1) {
self::$variable1 = $variable1;
}
public static function setVariable2($variable2) {
self::$variable2 = $variable2;
}
}
And the include this class in file, where you want to use them, and call variablesPlaceHolder::setVariable1(anyValue) variablesPlaceHolder::getVariable1()
So I re-wrote this to hopefully be clearer about what I'm attempting. During some parsing of data someValue(s) associated with someCode(s) are received. The intent is to capture a particularValue of a someCode having the lowest precedence, irrespective of the order the someCode/someValue (s) received. The issue keeping this from working is that the closure is created every time the private function is called and the value of $precedenceOfCodeCaptured therefore is always reset to null. If I could keep the closure around then things might work as intended.
private function Foo($particularValue, $someValue, $someCode) {
switch ($someCode) {
case:
CODE1:
case:
CODE2:
$c = function () use ($someCode, $someValue) {
static $precedenceOfCodeCaptured = null;
$precedenceArray = array(
CODE2 => 1,
CODE1 => 2
);
if ((is_null($someValue))) {//first time the case statement matched because $someValue==null
$precedenceOfCodeCaptured = $precedenceArray[$someCode];
$particularValue = someValue;
} else if ($precedenceArray[$someCode] <= $precedenceOfCodeCaptured) {
$particularValue = someValue;
}
};
$c();
break;
...
}
}//end of private method
Every time Foo is called, you create a new function/closure, since you evaluate a function(), and that's what evaluating function() does!
If you'd like to have Foo() return the same function/closure each time, only evaluate it once. Perhaps use a singleton pattern, where you'll check if some higher-scoped variable that holds the function has been initialized, and initialize it if not.
For example:
$c = null;
private function Foo(){
if ($c == null) {
$c = function() use ( $whatever){
static $x = 0;
echo "x= $x";
$x++;
...
};
}
$c();
}//end of private method
(but please don't use globals in your actual code!)
Try this:
static $x = 0;
private function Foo() {
$c = function() use ( $whatever){
echo "x= $x";
self::$x++;
...
};
$c();
}
<?php
class example{
private function foo(){
static $_closure;
if( ! $_closure ){
$_closure = function(){
static $_state;
/* . . . */
};
}
$_closure();
}
}
Note that the closure is static (it is created on first use, and not again), as is the variable that holds state inside the closure.
However, as stated in other answers, this is "pointless" at best. $_closure should be another class method; $_state should be a class property (there is no need for static anything in the closure, in foo(), nor in the class). It is not "cleaner" or more readable or performant (in fact, the opposite is likely true) to use a closure like this.
It might make sense if you were doing something like:
returning the closure for use elsewhere
dynamically creating the function at runtime (i.e., based on foo() args)
something else dynamic and interesting
…but as it stands, this is simply the wrong approach. The tools to do this already exist.
I have a class with a method that returns a new instance of itself.
This allows me to use one single object without having to declare new ones over and over with the same values. For those wondering why not extending the class or something else, it just makes my life easier and it works like a charm, but my final purpose is to remove them all in one line.
for example:
$foo = new myClass('name1','ab','cd');
$bar = $foo->duplicate();
//duplicate() saves the new object in itself in an array:
array_push($this->instance,new myClass('name1','ab','cd'));
//and returns a pointer to the saved instance.
return end($this->instance);
$foo and $bar now share the same methods and values but those values can be modified separately.
$foo->changeName('newName');
$bar->changeName('newName2');
My question here is, if I unset the first class created unset($foo) will PHP automatically unset the other instances ($bar) or will the garbage collector remove them eventually?
I have tested it by unsettling $foo and gives me an error when I call $foo but not when I call $bar.
What I am trying to do is to unset all the instances of that class at once.
Thanks.
Automatic cleanup?
No, PHP isn't awared about your architecture. It will not remove objects "cascade", they are independent entities - and, more, can belong to different scopes, for example. Simple code:
class Test
{
protected $id = null;
protected $uniqid = null;
public function __construct($id)
{
$this->id = $id;//user-passed variable
$this->uniqid = uniqid();//internal: to identify instance
}
public function getCopy()
{
return new self($this->id);
}
public function getIdentity()
{
return $this->uniqid;
}
}
$foo = new Test(3);
$bar = $foo->getCopy();
var_dump($foo->getIdentity());//valid
unset($foo);
//still valid: bar has nothing to do with foo
var_dump($bar->getIdentity());
By the way, for copying you can use clone in PHP (that, however, will result in object cloning, obviously)
Simple way
Most simple way to resolve a matter is to iterate through $GLOBALS, checking it with instanceof. This has serious weakness: inner function/method scopes would not be affected:
//static since doesn't belong to any instance:
public static function cleanup()
{
foreach($GLOBALS as $name=>$var)
{
if($var instanceof self)
{
unset($GLOBALS[$name]);
}
}
}
-and
$foo = new Test(3);
$bar = $foo->getCopy();
var_dump($foo->getIdentity(), $bar->getIdentity());//valid
Test::cleanup();
//2 x notice:
var_dump($foo, $bar);
Note, that is has nothing to do with "child" mechanics (i.e. it will clean all instances in global scope - no matter which was copied from which).
Common case
Sample above will not do the stuff in common case. Why? Imagine that you'll have holder class:
class Holder
{
protected $obj = null;
public function __construct($obj)
{
$this->obj = $obj;
}
public function getData()
{
return $this->obj;
}
}
and you'll pass instance to it:
$foo = new Test(3);
$bar = $foo->getCopy();
$baz = new Holder($bar);
-so then you'll have no chances to handle even this simple situation in common case. And with more complex situations you will also be stuck.
What to do?
I'd recommend: destroy objects explicitly when you need to do that. Implicit unset is a side-effect, and even if you'll maintain that somehow (I can imagine Observer pattern + some global registry for that) - it will be horrible side-effect, that will kill readability for your code. And same is about code, that uses $GLOBALS I've written above - I do not recommend to act such way in any case.
Try the below code to clear all php objects.
public function clearAllVars()
{
$vars = get_object_vars($this);
foreach($vars as $key => $val)
{
$this->$key = null;
}
}
}
From what I understand from the PHP reference here :
http://www.php.net/manual/en/features.gc.refcounting-basics.php
destroying the main instance will not automatically destroy all other instances since they do not ultimately point to the same 'zval'.
However the garbage collection will eventually destroy all instances if they are not referenced anymore. Since in your example $bar still references the second instance, it will not be destroyed.
What you could do if you want to unset them all at the same time :
Use a static array referencing all the instances of your object
Every time you create a new object add a reference to this object in the static array of the class
Use a static function unsetAll() which loops through this array and unset one by one all instances
It seems some people hate global variables, but if you can explain how to code without them, I'm all ears.
Otherwise, I have a few options and I'm curious which is the best long-term. Consider that I have a variable that won't change for the duration. It's a static global. I can use:
$_SESSION['var'] = constantval;
define('var', constantval);
var = constantval;
and the one I'm really curious about:
function my_constants($which)
{
switch ($which) {
case 'var':
return 'constantval';
}
}
In that last one, the goal is to keep variable out of global scope to save memory at the sacrifice of some processor cost. Is the memory saved worth the cycles? Is there a noteworthy difference between the various other types of global declaration?
Global variables are not considered a bad practice because of memory usage or processor cost. It's because of the problems that allowing any part of your program to modify them may cause. With the time, it becomes hard to understand which parts of the program read or write to your global variables.
Alternatives to globals (singetons).
It will give you a fine grained access control:
E.g.:
class ConfigParamSingelton {
private var $value;
private static $mInstance = null;
public function getValue() {
return $this->value;
}
public function getInstance() {
if(self::$mInstance == null) {
self::$mInstance = new ConfigParamSingelton();
}
return self::$mInstance;
}
So now you can either:
protected function setValue($val) { // is immuteable by clients of the object
$this->value = $val;
}
or
public function setValue($val) {// is muteable
$this->value = $val;
}
Well, this are singletons. You don't need globals in this case.
I'm not sure if this is a trivial questions but in a PHP class:
MyClass:
class MyClass {
public $var1;
public $var2;
constructor() { ... }
public method1 () {
// Dynamically create an instance variable
$this->var3 = "test"; // Public....?
}
}
Main:
$test = new MyClass();
$test->method1();
echo $test->var3; // Would return "test"
Does this work?? How would I get this to work? Ps. I wrote this quickly so please disregard any errors I made with setting up the class or calling methods!
EDIT
What about making these instance variables that I create private??
EDIT 2
Thanks all for responding - Everyone is right - I should have just tested it out myself, but I had an exam the next morning and had this thought while studying that I wanted to check to see if it worked. People keep suggesting that its bad OOP - maybe but it does allow for some elegant code. Let me explain it a bit and see if you still think so. Here's what I came up with:
//PHP User Model:
class User {
constructor() { ... }
public static find($uid) {
$db->connect(); // Connect to the database
$sql = "SELECT STATEMENT ...WHERE id=$uid LIMIT 1;";
$result = $db->query($sql); // Returns an associative array
$user = new User();
foreach ($result as $key=>$value)
$user->$$key = $value; //Creates a public variable of the key and sets it to value
$db->disconnect();
}
}
//PHP Controller:
function findUser($id) {
$User = User::find($id);
echo $User->name;
echo $User->phone;
//etc...
}
I could have just put it in an associative array but I can never correctly name that array something meaningful (ie. $user->data['name'] ... ugly.) Either way you have to know what is in the database so I do not really understand what the argument is that its confusing, especially since you can just var dump objects for debugging.
Why dont you just write the code and see for yourself?
<?php
class Foo
{
public function __construct()
{
$this->bar = 'baz';
}
}
$foo = new Foo;
echo $foo->bar; // outputs 'baz'
and
var_dump($foo);
gives
object(Foo)#1 (1) {
["bar"] => string(3) "baz"
}
but
$r = new ReflectionObject($foo);
$p = $r->getProperty('bar');
var_dump($p->isPublic());
will throw an Exception about 'bar' being unknown, while
$r = new ReflectionObject($foo);
$p = $r->getProperties();
var_dump($p[0]->isPublic());
will return true.
Now, should you do this type of assignment? Answer is no. This is not good OOP design. Remember, OOP is about encapsulation. So, if bar is describing some public property of the class, make it explicit and declare it in your class as public $bar. If it is supposed to be private declare it as private $bar. Better yet, dont use public properties at all and make them protected and provide access to them only through getters and setters. That will make the interface much more clearer and cleaner as it conveys what interaction is supposed to be possible with an object instance.
Assigning properties on the fly here and there across your code, will make maintaining your code a nightmare. Just imagine somewhere along the lifecylce of Foo someone does this:
$foo = new Foo;
$foo->monkey = 'ugh';
echo $foo->monkey; // outputs 'ugh'
Now, from looking at the class definition above, there is absolutely no way, a developer can see there is now a monkey patched into Foo. This will make debugging a pain, especially if code like this is frequent and distributed across multiple files.
Yes that will indeed work. Auto-created instance variables are given public visibility.
yes that works as you'd hope/expect.
I you wanted to make private variables on the fly you could use php magic functions to emulate this, e.g
MyClass
<?php
class MyClass {
public $var1;
public $var2;
private $data = array();
public function __get($key) {
// for clarity you could throw an exception if isset($this->data[$key])
// returns false as it is entirely possible for null to be a valid return value
return isset($this->data[$key]) ? return $this->data[$key] : null;
}
public function __set($key, $value) {
$this->data[$key] = $value;
}
}
?>
Main
<?php
$test = new MyClass();
$test->myVar = 'myVar is technically private, i suppose';
echo $this->myVar; // 'myVar is technically private
?>
Although these dynamically created variables are technically private, they are infact publicly accessible... i cannot image the purpose for wanting to dynamically create private instance variables. I would question your design.
Did you try it?
It is possible but you might get strict errors. If you dynamically need to create these variables, you are probably doing something wrong.
You should either change this into a function:
function var($no) { .. }
or use __get (http://ca.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members)