Is it possible to handle this type of errors? Something like spl_autoload_register, but for functions.
Basically, what I am trying to do:
I have the class:
class Foo {
public function bar() {
echo 1;
}
}
So, when I call a nonexistent function Foo() like this:
Foo()->bar();
The possible handler should create a function Foo(), which looks like that:
function Foo() {
return new Foo();
}
If you never need an actual instance of the object, why not use a static class?
class Foo {
public static function bar() {
echo 1;
}
}
Foo::bar();
Then, you can do this in your app:
$this->fiends = FriendsModel::getUserFriends($userId);
Related
How can I create something like
MyObject->property->method()
in PHP?
I only know how to create a method for a class:
class MyObject
{
public function MyMethod()
{
// do something
}
}
In Javascript I can easily do something like
var MyObject = {
property : {
method : function ()
{
// do something
}
}
}
How do I do that?
In Javascript you can create objects and methods inline, in PHP you need to have a class and instantiate it:
class Foo {
public function method() {}
}
class MyObject {
public $property;
public function __construct() {
$this->property = new Foo;
}
}
$o = new MyObject;
$o->property->method();
You can set an object as the value of a property. Something like this:
class Foo {
public $Bar;
public function __construct() {
$this->Bar = new Bar();
}
}
class Bar {
public function ShowBar() {
echo 'Bar';
}
}
$Foo = new Foo();
$Foor->Bar->ShowBar();
As others have correctly answered, this works differently in PHP and Javascript. And these differences are also the reason why in PHP you need to define the class methods before you run them. It might become a bit more dynamic in the future but I'm sure not on the level of Javascript.
You can however fake this a bit in PHP because you can assign functions to properties dynamically:
$myObject = new PropCall;
$myObject->property->method = function() {
echo "hello world\n";
};
$myObject->property->method();
This example outputs:
hello world
This does work because some little magic has been added in the instantiated object:
class PropCall
{
public function __call($name, $args) {
if (!isset($this->$name)) {
return null; // or error handle
}
return call_user_func_array($this->$name, $args);
}
public function __get($name) {
$this->$name = new PropCall;
return $this->$name;
}
}
This class code checks if a dynamic property has been added with the name of the method called - and then just calls the property as a function.
I'm new to programming. I have this going on:
I have Class A, which have many functions. One of those functions is functionX.
In functionX I need to make a call to functionY which belongs to another class: Class B.
So how do I acces to functionY from inside functionX?
I use Codeigniter.
Thanks in advance.
Try and experiment with this.
class ClassA {
public function functionX() {
$classB = new ClassB();
echo $classB->functionY();
}
}
class ClassB {
public function functionY() {
return "Stahp, no more OO, stahp!";
}
}
Class function? A static method?
If you have an instance (public) method, you just call $classB->functionY().
If you have a static method, you would call ClassB::functionY();
So:
class ClassA {
public function functionX(){
$classB = new ClassB();
// echo 'foo';
echo $classB->functionY();
// echo 'bar';
echo ClassB::functionYStatic();
}
}
class ClassB {
public $someVar;
public static $someVar2 = 'bar';
function __construct(){
$this->someVar = 'foo';
}
public function functionY(){
return $this->someVar;
}
public static function functionYStatic(){
return self::$someVar2;
}
}
Well that depends. If that function is a static function or not.
First off you must include the file with the class...
include_once('file_with_myclass.php');
If it is static you can call it like this:
ClassName::myFunction()
If it is not, then you create an instance of the class and then call the function on that instance.
$obj = new ClassName();
$obj->myFunction();
As you can guess the function being static means you can call it without the need of creating an instance. That is useful for example if you have a class Math and want to define a function that takes to arguments to calculate the sum of them. It wouldn't really be useful to create an instance of Math to do that, so you can declare as static and use it that way.
Here's a link to the docs with further info
http://www.php.net/manual/en/keyword.class.php
If functionY is static you can call ClassB::functionY(). Else you must create instance of Class B first. Like:
$instance = ClassB;
$instance->functionY();
But maybe you mean something else?
Looks like one of your class has a dependency to another one:
<?php
class A
{
public function x()
{
echo 'hello world';
}
}
class B
{
private $a;
public function __construct(A $a)
{
$this->a = $a;
}
public function y()
{
$this->a->x();
}
}
$a = new A();
$b = new B($a);
$b->y();
Depending how your code looks like, if it makes sense, you can inject class A into y()
public function y(A $a)
{
// your code with $a
}
I have the following example code
<?php
class Test {
function foo() {
print "foo\n";
}
function bar() {
$func = 'foo';
$func();
}
}
$test = new Test();
$test->bar()
which calls $test-bar(), whiich internally calls a variable php function named foo. This variable contains the string foo and I want the function foo be called like here. Instead of getting the expected output
foo
I get an error:
PHP Fatal error: Call to undefined function foo() ...
How to do this right, when using a string for the function-name? The string 'func' might denote several different functions inside the class scope in the actual code.
According to the doc the above should work like I have coded, more or less...
<?php
class Test {
public function foo() {
print "foo\n";
}
public function bar() {
$func = 'foo';
$this->$func();
}
}
$test = new Test();
$test->bar();
?>
Use this for accessing the current function of this class
What you can do is use the function call_user_func() to invoke the callback.
<?php
class Test {
public function foo() {
print "foo\n";
}
public function bar() {
$func = 'foo';
call_user_func(array($this, $func));
}
}
$test = new Test();
$test->bar();
You use the keyword $this
<?php
class Test {
function foo() {
print "foo\n";
}
function bar() {
$this->foo(); // you can do this
}
}
$test = new Test();
$test->bar()
There are two ways to call a method from a string input:
$methodName = "foo";
$this->$methodName();
Or you can use call_user_func_array()
call_user_func_array("foo",$args); // args is an array of your arguments
or
call_user_func_array(array($this,"foo"),$args); // will call the method in this scope
Can you do something like this in PHP:
function foo()
{
super->foo();
// do something
}
Yes, it's called parent:: though.
public function foo()
{
parent::foo(); // this is not a static method call, even though it looks like one
//do something
}
use parent;
parent::foo();
Do you mean calling the parent class method? In that case you would do:
class Bar
{
public function foo()
{
// blah
}
}
class Baz extends Bar
{
public function foo()
{
parent::foo();
}
}
I am trying to figure out how to catch any method called on an object in PHP. I know about the magic function __call, but it is triggered only for methods that do not exist on the called object.
For example i have something like this:
class Foo
{
public function bar()
{
echo 'foobar';
}
public function override($method_name,$method_args)
{
echo 'Calling method ',$method_name,'<br />';
$this->$method_name($method_args); //dirty, but working
}
}
And when i do this:
$foo = new Foo();
$foo->bar();
I want this output:
Calling method bar
foobar
instead of this one:
foobar
Is there any way how to do this? Help please :)
Taking your original Foo implementation you could wrap a decorator around it like this:
class Foo
{
public function bar() {
echo 'foobar';
}
}
class Decorator
{
protected $foo;
public function __construct(Foo $foo) {
$this->foo = $foo;
}
public function __call($method_name, $args) {
echo 'Calling method ',$method_name,'<br />';
return call_user_func_array(array($this->foo, $method_name), $args);
}
}
$foo = new Decorator(new Foo());
$foo->bar();
You can wrap an object around the object, intercepting any calls then forwarding them on the original object and returning the result.
Just store the object as a variable in your wrapper class and use overloading methods in your wrapper class to call/set/get/check on the object.
$object = new AnyObject;
$object = new Wrapper($object);
$object->anyMethod();
$object->anyVar = 'test';
echo $object->anyVar;
echo $object['array form'];
Looping the wrapper class in foreach is probably harder. Havent tried that.
If you set the function to private , call will trap any call to it from the outside will be trapped in __call, but you can call it from the inside
class Foo
{
private function bar()
{
echo 'foobar';
}
public function __call($method_name,$method_args)
{
echo 'Calling method ',$method_name,'<br />';
$this->$method_name(); //dirty, but working
}
}