[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

why is $documentIndocument null?

Posted on 2014-04-23
7
Medium Priority
?
175 Views
Last Modified: 2014-04-23
why is $documentIndocument null?

output:
Document Object
(
    [inDomainObject] => 3
)
User Object
(
    [inUser] => 2
    [inDomainObject] => 1
)
User Object
(
    [inUser] => 2
    [inDomainObject] => 1
)



<?php
abstract class DomainObject {
    public $inDomainObject=1;
    public static function create() {
        return new static();        
    }
}

class User extends DomainObject {
   public $inUser=2;
   public static function inuserstatic(){
     return new static();
   }
}

class Document extends DomainObject {
  public $inDomainObject=3;
  public static function indocument(){
    self::create();
  }
}

echo '<pre>';
$documentCreate=Document::create();
print_r($documentCreate);
$userInuserstatic=User::inuserstatic();
print_r($userInuserstatic);
$userCreate=User::create();
print_r($userCreate);
$documentIndocument=Document::indocument();
print_r($documentIndocument);
echo '</pre>';
?>

Open in new window

0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40018961
Maybe I'm missing something...what is null? Your object appears to have been dumped out via print_r without issue.
0
 

Author Comment

by:rgb192
ID: 40018981
$documentIndocument=Document::indocument();
print_r($documentIndocument);

is null

notice there are 4 variables and only 3 dumps
0
 

Author Comment

by:rgb192
ID: 40018997
<?php
abstract class DomainObject {
    public $inDomainObject=1;
    public static function create() {
        return new static();        
    }
}

class User extends DomainObject {
   public $inUser=2;
   public static function inuserstatic(){
     return new static();
   }
}

class Document extends DomainObject {
  public $inDocumentObject=3;
  public static function indocument(){
    self::create();
  }
}

echo '<pre>';
$documentCreate=Document::create();
print_r($documentCreate);
$userInuserstatic=User::inuserstatic();
print_r($userInuserstatic);
$userCreate=User::create();
print_r($userCreate);
$documentIndocument=Document::indocument();
print_r($documentIndocument);
echo '</pre>';
?>

Open in new window


I change line 17
from
public $inDomainObject=3;
to
public $inDocumentObject=3;

for clarity so we can tell which class was entered.

new output

Document Object
(
    [inDocumentObject] => 3
    [inDomainObject] => 1
)
User Object
(
    [inUser] => 2
    [inDomainObject] => 1
)
User Object
(
    [inUser] => 2
    [inDomainObject] => 1
)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40019014
Ah, my mistake. (So I was missing something!) The problem is that you are not returning anything from your method call. Change line 19 to:

return self::create();

Open in new window

0
 

Author Comment

by:rgb192
ID: 40019103
but why doesnt the current line19
self::create();
return line5
which is
return new static();

why do I need to append 'return' to line19 when there is already a 'return' in line5
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40019136
Because each function returns something. A function calling another function doesn't mean that the called function's return value will automatically be returned from the calling function. If you write a function that you want to return values from, then it must have a return itself.
0
 

Author Closing Comment

by:rgb192
ID: 40019181
thanks for teaching me about return values of a function.
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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 …
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.
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.

649 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