Solved

pass by reference

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

9 Experts available now in Live!

Get 1:1 Help Now