Solved

why is $documentIndocument null?

Posted on 2014-04-23
7
173 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
Industry Leaders: 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 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 500 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

Technology Partners: 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!

Question has a verified solution.

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

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 article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

717 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