Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

pass by reference

Posted on 2002-06-13
6
Medium Priority
?
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 5

Expert Comment

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

Accepted Solution

by:
Hamlet081299 earned 400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

618 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