Solved

why is $documentIndocument null?

Posted on 2014-04-23
7
170 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
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 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 …

749 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