Solved

PHP 5 Constructors

Posted on 2004-07-31
9
444 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 …

895 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