?
Solved

PHP 5 Constructors

Posted on 2004-07-31
9
Medium Priority
?
449 Views
Last Modified: 2006-11-17
Why is it that I cannot pass a variable to a constructor in PHP 5?

Example:

class Test {

function __construct($constructvar)
{
echo $constructvar;
}

}

$Test = new Test("hi");

if(class_exists($Test))
echo "Test has been initialized.";
else
echo "No go.";

Any ideas? Or have I forgot to remember something about constructors...
0
Comment
Question by:drakkarnoir
[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
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 11685470
class_exists is looking for a string name not an initialized class, you would have to do

<?php
class Test {
 function __construct($constructvar) {
  echo $constructvar;
 }
}

$Test = new Test;
$Test -> __construct("hi");

echo (class_exists('test')) ? "Test has been initialized." : "No go.";
?>
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11685495
The only way you can feed it as a variable is by assigning the string name of the class like this

class Test {
 function __construct($constructvar) {
  echo $constructvar;
 }
}

$Test = Test;

echo (class_exists($Test)) ? "Test has been initialized." : "No go.";
0
 
LVL 2

Expert Comment

by:blackelvis
ID: 11686343
a constructor is executed when you create an instance, so there cannot be the possibility of passing a variable. if you need to, you'll have to stick to calling a selfmade constructor method.
0
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 2

Expert Comment

by:blackelvis
ID: 11686349
...or set the initial value for your variable in your properties declaration. but this abiously lacks the flexibility you are probably looking for.
0
 
LVL 2

Accepted Solution

by:
d_tan earned 2000 total points
ID: 11690224
Uhhh. . .ya.

Basically all you are doing is using the class_exists method wrong. . .

You CAN still pass in parameters into the default constructor.  

######################################3
class Test {
      public $val;
 function __construct($constructvar) {
        $this->val = $constructvar;
 }
}

$test = new Test( "hello" );

echo $test->val;

if (class_exists("Test")) {
    echo "Yup exists";
}
else {
      echo "Nope";
}

#####################
The previous code passes in a variable into the constructor and then echos that public variable.  Notice that when I call class_exists I use a string which is the NAME of the class.  class_exists checks to see if the class has been DEFINED, not instantiated.

dtan
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11696509
i dont see an awful lot of difference between the accepted answer and mine :S

anyone care to shed some light...
0
 
LVL 2

Expert Comment

by:d_tan
ID: 11699707
I addressed the fact that the question was actually  Why is it that I cannot pass a variable to a constructor in PHP 5? vs.  Why isn't class_exists working.  I think points could have been split.

dtan
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 11699716
I had thought i had highlighted the reason why it wasnt working in my first and second post

>>  class_exists is looking for a string name not an initialized class

and

>>  The only way you can feed it as a variable is by assigning the string name of the class like this...

oh well :(
0
 
LVL 2

Expert Comment

by:d_tan
ID: 11699945
I think he was more interested in why

 function __construct($constructvar) {

}

wasn't working. . . but mistakenly used class_exists .

dtan
0

Featured Post

Industry Leaders: 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

This article discusses how to create an extensible mechanism for linked drop downs.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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