Solved

PHP 5 Constructors

Posted on 2004-07-31
9
445 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
  • 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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 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…

773 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