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();
}
}
Related
guys lets say we have this class: all i want to do is to use a specific function to do something, think it as a button, but in a way that you have 1 public function and you can execute parts of it!
CLASS DATA {
public function X() {
function A() {
//do_something
}
}
}
and now i'm in the index.php and i want to call the function A() only.
i tried $data->X()->A() but nothing
i tried $data->X(A()) also nothing
is it possible this?
In the way you've written it, no. Looks to me like you're trying to build something in the way you would a Javascript application. But like Rizier123 points out you could do something like this.
class Foo {
public function getBar(){
return new Bar();
}
}
class Bar {
public function someFunction() {
}
}
$foo = new Foo();
$foo->getBar()->someFunction();
Although I'm not entirely sure why you would want to nest things that way when inheriting would be a better route. Something like this:
class Foo extends Bar {
}
class Bar {
public function someFunction() {
}
}
$foo = new Foo();
$foo->someFunction();
But I guess you could use the former as a way to pass specific constructor parameters in a consistent manor.
class Foo {
public function getRainbow(){
return new Bar('rainbow');
}
}
class Bar {
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function someFunction() {
switch($this->type){
case 'rainbow':
echo 'All the way across the sky.';
break;
default:
echo 'Boring.';
break;
}
}
}
$foo = new Foo();
$foo->getRainbow()->someFunction();
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.
Is it possible to create a function out of a class method?
ie.
class Test {
public function __construct()
{
if ( ! function_exists('foo') ) {
function foo ()
{
return $this->foo();
}
}
}
private function foo()
{
return 'bar';
}
}
Or would I have to do it the other way around, creating a function and using it within the method?
Just do it like that, php is able to do it
class Test {
public function __construct()
{
if ( ! function_exists('foo') ) {
function foo ()
{
return $this->foo();
}
}
}
private function foo()
{
outsidefunction();
return 'bar';
}
}
private function outsidefunction()
{
return 0;
}
i'm trying to create a global function that is a copy of the class method. I come from javascript land where functions are just variables and you can easily copy them...
Functions in PHP are not first class citizens, you cannot copy functions around like variables in PHP. You can hand a reference to a function around, but not the function itself.
In theory, you could use Reflection to get a Closure, reference it via $GLOBALS and then define a function foo to call the Closure from $GLOBALS, e.g.
<?php // requires 5.4
class Test {
public function __construct()
{
if (!function_exists('foo')) {
$reflector = new ReflectionMethod(__CLASS__, 'foo');
$GLOBALS['foo'] = $reflector->getClosure($this);
function foo() {
return call_user_func($GLOBALS['foo']);
}
}
}
private function foo()
{
return 'bar';
}
}
$test = new Test();
echo foo();
Run Demo
However, that is extremely ugly and you do not want to do it this way.
If you want more JavaScript like objects, have a look at
http://www.phpied.com/javascript-style-object-literals-in-php
But still, even the suggested techniques in there are kludges imo.
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);
I know what self::staticFunctionName() and parent::staticFunctionName() are, and how they are different from each other and from $this->functionName.
But what is static::staticFunctionName()?
It's the keyword used in PHP 5.3+ to invoke late static bindings.
Read all about it in the manual: http://php.net/manual/en/language.oop5.late-static-bindings.php
In summary, static::foo() works like a dynamic self::foo().
class A {
static function foo() {
// This will be executed.
}
static function bar() {
self::foo();
}
}
class B extends A {
static function foo() {
// This will not be executed.
// The above self::foo() refers to A::foo().
}
}
B::bar();
static solves this problem:
class A {
static function foo() {
// This is overridden in the child class.
}
static function bar() {
static::foo();
}
}
class B extends A {
static function foo() {
// This will be executed.
// static::foo() is bound late.
}
}
B::bar();
static as a keyword for this behavior is kind of confusing, since it's all but. :)