Solved

pass by reference

Posted on 2002-06-13
6
209 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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.

785 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