Solved

pass by reference

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 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