Solved

PHP 5 Constructors

Posted on 2004-07-31
9
447 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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