Solved

why is $documentIndocument null?

Posted on 2014-04-23
7
164 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
$documentIndocument=Document::indocument();
print_r($documentIndocument);

is null

notice there are 4 variables and only 3 dumps
0
 

Author Comment

by:rgb192
Comment Utility
<?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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
thanks for teaching me about return values of a function.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
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 …

744 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

13 Experts available now in Live!

Get 1:1 Help Now