Solved

new static(), new self()

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use md5 hashing 3 26
Hacked File Timestamps 4 49
Mysql sync between 3-4 mysql db 4 24
myqsl update statement on phpMyAdmin 8 21
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

919 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

17 Experts available now in Live!

Get 1:1 Help Now