Solved

new static(), new self()

Posted on 2013-12-09
8
579 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 109

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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 109

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 500 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 109

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now