[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

why is $documentIndocument null?

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
rgb192
Asked:
rgb192
  • 4
  • 3
1 Solution
 
käµfm³d 👽Commented:
Maybe I'm missing something...what is null? Your object appears to have been dumped out via print_r without issue.
0
 
rgb192Author Commented:
$documentIndocument=Document::indocument();
print_r($documentIndocument);

is null

notice there are 4 variables and only 3 dumps
0
 
rgb192Author Commented:
<?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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
käµfm³d 👽Commented:
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
 
rgb192Author Commented:
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
 
käµfm³d 👽Commented:
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
 
rgb192Author Commented:
thanks for teaching me about return values of a function.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now