Solved

PHP 5 Constructors

Posted on 2004-07-31
9
443 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

12 Experts available now in Live!

Get 1:1 Help Now