Solved

pass by reference

Posted on 2002-06-13
6
213 Views
Last Modified: 2010-05-19
Why doesn't the function "printKlasse1()"in Klasse2 print ??



<?php

class Klasse1 {
  var $var;
  function Klasse1() {
   $this->var = "Hello World";
  }
}


class Klasse2 {
  var $klasse1;

  function Klasse2(&$arg) {
    $this->klasse1 = &$arg;
  }
  function printKlasse1() {
    print $klasse1->var;
  }
}

class Klasse3 {
  function Klasse3() {
    $klasse1 = new Klasse1();
    $klasse3 = new Klasse2($klasse1);
  }
}
$klasse3 = new Klasse3();
?>
0
Comment
Question by:df_n
  • 2
6 Comments
 
LVL 5

Expert Comment

by:Hamlet081299
ID: 7076916
Because it is never called!!??
0
 
LVL 5

Accepted Solution

by:
Hamlet081299 earned 100 total points
ID: 7076928
I think a big part of the problem is that you have perhaps missed a number of 'this->' references.

This seems to work...

<?php

class Klasse1 {
 var $var;
 function Klasse1() {
  $this->var = "Hello World";
 }
}


class Klasse2 {
 var $klasse1;

 function Klasse2(&$arg) {
   $this->klasse1 = &$arg;
 }
 function printKlasse1() {
   print $this->klasse1->var;
 }
}

class Klasse3 {
 function Klasse3() {
   $this->klasse1 = new Klasse1();
   $this->klasse3 = new Klasse2($this->klasse1);
 }
}
$klasse3 = new Klasse3();
$klasse3->klasse3->printKlasse1();
?>
0
 
LVL 8

Expert Comment

by:us111
ID: 7090212
hmm I think you want to have a class and some extension of this class that's why your code doesn't work.

have a look at
http://www.php.net/manual/en/ref.classobj.php for more explanations.

<?php

class Klasse1 {
 var $var;

 function Klasse1($value="") {
  $this->var = $value;
 }
}


class Klasse2 extends Klasse1 {

 function Klasse2($arg="") {
   $this->var = $arg;
 }
}

$test = new Klasse2("test");
print $test->var;
?>

0
 
LVL 11

Expert Comment

by:shmert
ID: 9643424
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept: hamlet
Please leave any comments here within the next seven days.
               
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
               
Sam Barnum
EE Cleanup Volunteer              
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

821 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