Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

new static(), new self()

Posted on 2013-12-09
8
Medium Priority
?
599 Views
Last Modified: 2013-12-13
<?php
//Abstract class HouseholdObject{
  
//}
class Couch extends HouseholdObject{
  Public static function create(){
    return new Couch();
  }
}
class FlatScreenTV extends HouseholdObject{
  Public static function create(){
    return new FlatScreenTV();
  }
}
class Refrigerator extends HouseholdObject{
  Public static function create(){
    return new Refridgerator();
  }
}
Abstract class HouseholdObject{
  Public static function create(){
    return new static();
  }
}
//class Couch extends HouseholdObject{
  
//}
class Table extends HouseholdObject{
  
}
//class Refrigerator extends HouseholdObject{
  
//}
Couch::create();
Table::create();
echo '<br>end';

Open in new window



from php object oriented tutorial


line 22:
what is the difference between return new static(); and return new self();
0
Comment
Question by:rgb192
  • 3
  • 3
  • 2
8 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39708240
The first thing you want to do is look at the data.  What do you get when you use var_dump() to print out the return values.
0
 
LVL 19

Expert Comment

by:Barthax
ID: 39708255
Both are confined to the idea of which object they refer to.  self() always refers to the object in which self() is coded.  static() refers to the current instantiated (potentially child) version of the original even if that child object does not alter the method of the parent.

Examples from http://php.net/lsb :
<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
?>

Open in new window

Self refers to the object in which it was coded and will output A.

<?php
class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        static::who(); // Here comes Late Static Bindings
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();
?>

Open in new window

Static refers to the instantiated object and will output B.
0
 

Author Comment

by:rgb192
ID: 39709284
Code from Barthax works.

The first thing you want to do is look at the data.  What do you get when you use var_dump() to print out the return values.


How?

Couch::create();
Table::create();

var_dump(Couch::create();)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39709333
Wow, good question.  I just installed this and ran it, and you don't have any assignment operators, so it creates no data.  What did you expect it to do?
0
 
LVL 19

Accepted Solution

by:
Barthax earned 2000 total points
ID: 39711409
Going with your original, perhaps the below will aid in understanding what happens when coupled with new:

<?php
class Couch extends HouseholdObject{
  function who(){
	echo get_class()."\n";
  }
}

class Refrigerator extends HouseholdObject{
  Public static function createself(){
    return new self();
  }
  function who(){
	echo get_class()."\n";
  }
}

class BigRefrigerator extends Refrigerator{
  function who(){
	echo get_class()."\n";
  }
}

Abstract class HouseholdObject{
  Public static function createstatic(){
    return new static();
  }

  // The existence of this would create a Fatal PHP error
  // As it is inside an Abstract object - which has no "self" capability.
  //  Public static function createself(){
  //    return new self();
  //  }

  function who(){
	echo get_class()."\n";
  }
}

$couchstatic = Couch::createstatic();
$bigfridgestatic = BigRefrigerator::createstatic();
$bigfridgeself = BigRefrigerator::createself();

$couchstatic->who();
$bigfridgestatic->who();
$bigfridgeself->who();

?>

Open in new window

This will output:
Couch
BigRefrigerator
Refrigerator

Open in new window

.. because the call to self is coded inside the Refrigerator object, a Refrigerator is created instead of a BigRefridgerator.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39711621
@Barthax: +1 for a great example.  I wish EE had a way for us to cause answers like this to percolate up to the top!
0
 
LVL 19

Expert Comment

by:Barthax
ID: 39711848
Many thanks, Ray_Paseur.  If such a feature existed, there's a fair-few of your answers I'd like to have seen at the top too. :)
0
 

Author Closing Comment

by:rgb192
ID: 39718051
thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question