Solved

new static(), new self()

Posted on 2013-12-09
8
567 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Accepted Solution

by:
Barthax earned 500 total points
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
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
Comment Utility
thanks
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

11 Experts available now in Live!

Get 1:1 Help Now