Do variable functions get overwritten by actual functions? - php

I know that you can call functions using variable names like this:
$foo = "bar";
function bar()
{
echo "test";
}
$foo(); // echos test
I am wondering if in a class, an actual function overrides that.
If I had this class:
class myClass{
public $test;
public function __construct()
{
$this->test = new myOtherClass;
}
public function test()
{
echo "foo";
}
}
Would both of these work correctly?
$obj->test(); // echo foo
$obj->test->method(); // access a method of myOtherClass
Edit: The original idea for this was that myOtherClass held a class with one main function that was accessed all the time, and a few other less accessed ones. I could use test() to link the main function to the class so there is less typing. But given the first answer I'll probably stick away from that.

I don't know PHP well enough to answer your question, but I spent several years maintaining products and would like to point out a serious difficulty with maintainability with this. Consider that you have the question as to whether this will even work properly. Now consider that the person maintaining your code will (a) have the same question as you and (b) probably not read the whole class (unless it's a very small class). And if you're changing $test from outside the class, it will be even more difficult to understand.
While this is an interesting question, especially from an academic point of view, it's a terrible practice from a maintenance point of view. Please use different variable names for different purposes; for example, use $otherClass for the pointer to the other class and test() for the test function and your maintainers (including yourself, if you maintain your own code) will curse at you less :)

PHP allows different symbols with the same name. Object properties and methods are totally different things in PHP, unlike JavaScript and some other languages:
// all of them work OK
define('SomeClass', 'SomeClass');
function SomeClass () {}
class SomeClass {}
This causes ugly problems in PHP 5.3:
$foo = new StdClass;
$foo->bar = function () {
return "bar";
};
$foo->bar(); // does not work, unfortunately :(

You wrote:
$obj->test(); // echo foo
$obj->test->method(); // access a method of myOtherClass
Right, this is how it works. You're not using anything variable in there.
$obj->test; // member variable $test of $obj
$obj->test(); // method test() of $obj
$obj->$test(); // variable function, result depends on content of $test
$obj->test()->otherTest(); // invokes otherTest() on an object
// returned by $obj->test()
$foo = 'test';
$bar = 'otherTest';
$obj->$foo()->$bar(); // same as above, but please, for the love of God,
// don't ever use this. ;)

Related

Inheritance bug in PHP [duplicate]

I've been looking at some code and am having a hard time working out variable declaration in php classes. Specifically it appears that the code i'm looking at doesn't declare the class variables before it uses them. Now this may be expected but I can't find any info that states that it is possible. So would you expect this:
class Example
{
public function __construct()
{
$this->data = array();
$this->var = 'something';
}
}
to work? and does this create these variables on the class instance to be used hereafter?
This works the same as a normal variable declaration would work:
$foo = 'bar'; // Created a new variable
class Foo {
function __construct() {
$this->foo = 'bar'; // Created a new variable
}
}
PHP classes are not quite the same as in other languages, where member variables need to be specified as part of the class declaration. PHP class members can be created at any time.
Having said that, you should declare the variable like public $foo = null; in the class declaration, if it's supposed to be a permanent member of the class, to clearly express the intent.
So would you expect this: (code sample) to work?
Yes. It's pretty bad practice (at least it makes my C++ skin crawl), but it wouldn't surprise me in the slightest. See example 2 in the following page for an example of using another class without declaring it beforehand. http://www.php.net/manual/en/language.oop5.basic.php It will throw an error if E_STRICT is enabled.
And does this create these variables on the class instance to be used hereafter?
Yep. Ain't PHP Fun? Coming from a C++/C# background, PHP took a while to grow on me with its very loose typing, but it has its advantages.
That's completely functional, though opinions will differ. Since the creation of the class member variables are in the constructor, they will exist in every instance of the object unless deleted.
It's conventional to declare class member variables with informative comments:
class Example
{
private $data; // array of example data
private $var; // main state variable
public function __construct()
{
$this->data = array();
$this->var = 'something';
}
}

Is it in any way possible to access a method-specific variable that is not a property of a class when extending a class?

Pleace consider the following example:
class A {
public method() {
$foo = 'bar';
}
}
class B extends A {
public method {
parent::method();
echo $foo; // $foo == null
}
}
I run to situations like this quite frequently when I need to extend the functionality of a class coded by somebody else. Is there a way to have the $foo of the aforementioned example to be not null but bar?
I know perfectly well, that what I'm suggesting is not a problem at all if the variable $foo were a property of the class. However, this example applies to a situation where the class I want to extend is poorly designed and modifying the original class and changing the rogue variables into class properties is out of the question for reasons such as the class being a part of a CMS and update compatibility etc.
Unfortunately not. Once method() is called, the $foo variable is contained only within method(). You would have to change method() to either return $foo, or echo it itself. Once method() completes its run (which is quick, as all it does is create a variable), that local variable is then destroyed.
Assuming that B is supposed to extend A (it's not in your example although it does not matter much in this case...) there is nothing you can do without modifying class A: The $foo variable is only defined in the local scope of the method in class A.
You can:
Return it;
Make it a class property;
Output it (perhaps that is done already...) and use output buffering to capture it but that's kind of a hack.
But that is about it, without modifying class A you cannot retain or access $foo inside that method.
No. $foo in this case is a local variable, existing only while the method() call is actually running. When the method returns, the local variables are cleaned up/destroyed. And even if you did something like make it a static variable, it still would exist only in the scope of method() and not be accessible from the outside world.
You'd need to do something like:
class A {
public $foo;
function method() {
$this->foo = 'bar';
}
}
But even then, class A would not be aware of class B and couldn't inject foo's value into your B method's scope.

PHP create_function Instance variable - Unable to call anonymous function: Follow up

This is somewhat a follow up to a previous question - but I've distilled the question down and have the "works" vs. "doesn't work" cases narrowed down much more precisely.
My Goal:
I have a class MyClass that has an instance variable myFunction. Upon creating a MyClass object (instantiating), the constructor assigns the instance variable myFunction with the result of a call to create_function (where the code and args come from a db call).
Once this object of type MyClass is created (and stored as an instance variable of another class elsewhere) I want to be able to call myFunction (the instance variable anonymous function) from "anywhere" that I have the MyClass object.
Experimental Cases -- below is my highly simplified test code to illustrate what works vs. what doesn't (i.e. when the expected functionality breaks)
class MyClass extends AnotherClass {
public $myFunction;
function __construct() {
$functionCode = 'echo "NyanNyanNyan";';
$this->myFunction();
/*Now the following code works as expected if put in here for testing*/
$anonFunc = $this->myFunction;
$anonFunc(); //This call works just fine (echos to page)!
/*And if i make this call, it works too! */
self::TestCallAnon();
}
public function TestCallAnon() {
$anonFunc2 = $this->myFunction;
$anonFunc2();
}
}
However, if I do the following (in another file, it errors saying undefined function () in... within the Apache error log.
//I'm using Yii framework, and this is getting the user
//objects instance variable 'myClass'.
$object = Yii::app()->user->myClass;
$object->TestCallAnon(); // **FAILS**
or
$func = $object->myFunction;
$func(); // ** ALSO FAILS **
In addition, several variations of calls to call_user_func and call_user_func_array don't work.
If anyone is able to offer any insight or help that would be great :).
Thanks in advance!
You can't pass references to functions around in PHP like you can in for instance JavaScript.
call_user_func has limited functionality. You can use it like so:
class MyClass {
function func() {}
static function func() {}
}
function myfunc() {}
$i = new MyClass();
call_user_func("myfunc", $args);
call_user_func(array($i, "func"), $args);
call_user_func(array(MyClass, "staticFunc"), $args);
I ended up solving this issue via a workaround that ended up being a better choice anyways.
In the end I ended up having a static class that had a method to randomly return one of the possible identifiers, and then another method which accepted that identifier to build the anonymous function upon each class.
Slightly less elegant than I would like but it ends up working well.
Thanks to everyone for your efforts.

implicit class variable declaration in php?

I've been looking at some code and am having a hard time working out variable declaration in php classes. Specifically it appears that the code i'm looking at doesn't declare the class variables before it uses them. Now this may be expected but I can't find any info that states that it is possible. So would you expect this:
class Example
{
public function __construct()
{
$this->data = array();
$this->var = 'something';
}
}
to work? and does this create these variables on the class instance to be used hereafter?
This works the same as a normal variable declaration would work:
$foo = 'bar'; // Created a new variable
class Foo {
function __construct() {
$this->foo = 'bar'; // Created a new variable
}
}
PHP classes are not quite the same as in other languages, where member variables need to be specified as part of the class declaration. PHP class members can be created at any time.
Having said that, you should declare the variable like public $foo = null; in the class declaration, if it's supposed to be a permanent member of the class, to clearly express the intent.
So would you expect this: (code sample) to work?
Yes. It's pretty bad practice (at least it makes my C++ skin crawl), but it wouldn't surprise me in the slightest. See example 2 in the following page for an example of using another class without declaring it beforehand. http://www.php.net/manual/en/language.oop5.basic.php It will throw an error if E_STRICT is enabled.
And does this create these variables on the class instance to be used hereafter?
Yep. Ain't PHP Fun? Coming from a C++/C# background, PHP took a while to grow on me with its very loose typing, but it has its advantages.
That's completely functional, though opinions will differ. Since the creation of the class member variables are in the constructor, they will exist in every instance of the object unless deleted.
It's conventional to declare class member variables with informative comments:
class Example
{
private $data; // array of example data
private $var; // main state variable
public function __construct()
{
$this->data = array();
$this->var = 'something';
}
}

Why is the usage of $this in PHP necessary when referencing methods or variables in the same class?

I was explaining to a Java developer why his method call wasn't working. He just needed to add $this->method_name();
He then asked me, "Why do I need to add $this to the method when it's declared in the same class?"
I didn't really know how to answer. Maybe it's because PHP has a global namespace and it you need to explicitly tell it that the method you are looking for belongs to the current class? But then why doesn't PHP check the current class for the method BEFORE looking at the global namespace?
The problem would also be that if you declared a function foo() and a method foo(), php would have a hard time figuring out which you meant - consider this example:
<?php
function foo()
{
echo 'blah';
}
class bar
{
function foo()
{
echo 'bleh';
}
function bar()
{
// Here, foo() would be ambigious if $this-> wasn't needed.
}
}
?>
So basically you can say that PHP - because of its "non-100%-object-orientedness" (meaning that you can also have functions outside classes) - has this "feature" :)
If I have to guess: Because it was easier than the alternatives. Object oriented support in PHP has always been very much of a hack. I vaguely remember reading a discussion about the upcoming closure support that will appear in PHP 5.3. Appearently it was really, really hard to implement lexical closures in PHP due to it's scoping rules. Probably because you can nest a class in a function in another class and stuff like that. All that freedom possibly makes stuff like this incredibly hard.
This is not unusual. Python, Javascript, Perl (and others) all make you refer to a this or self when dealing with objects.
That's just how scope works in PHP. $obj->f() refers to $foo in the function scope. If you want to get the class property $obj->foo within f(), it's $this->foo.
global $foo;
$foo = 99;
class myclass
{
public $foo;
function f()
{
$this->foo = 12;
$foo = 7;
// $this->foo != $foo != $GLOBALS['foo']
}
}
$this refers to the calling object. The PHP docs have good examples and further details.
Seems PHP hasn't been properly OOPed. In Java and C++, references to the current object ('this') are implicit, ie no explicit mention is needed, leaving the code much cleaner.
Perhaps there is some reason this is difficult with PHP implementation ?

Categories