• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Few PHP quesitons...

PHPForever

n00b
Joined
Jan 11, 2005
Messages
22
Say you have the following class (in PHP 5):

PHP:
<?php
class x
{
  function __constructor()
  {
    //init properties
  } 
  public function manage_class()
  {
    $x= new x();
    // do something here
  } 
}
?>
Is there a technical term for the *trick* used to initialize the class within itself?

If public is called from an external source like this x::manage_class(), how many instances of the class exist?
 
For all that is sacred please use the code tags to format code.
 
first, php 5 uses __construct not __constructor as per
http://us2.php.net/manual/en/language.oop5.decon.php

when you say x::manage_class(), you're intending to call a static method, but you need to define manage_class() as static
public static function manage_class() ...

if you call it statically, then there will be one instance of the class within the method when you call it. it dies once the method finishes running
 
doh said:
For all that is sacred please use the code tags to format code.

In the four years I've been here, same as you, I've come to the realization nothing is sacred anymore.

Code tags simply don't exist. Neither do forum stickies.
 
i should mention that it is only because it was a couple of lines that i even bothered looking. usually i would also tell the op to use code/php tags ;)
 
If you didn't return the value of $x (btw, you are missing a var $x), then after the manage)class() method runs, it will remove anything created, hence, 0 classes.
 
First, I want to apologize for not using the PHP code tags. Honestly I never before (except my last post) posted any code here. Second, I want to thank those that actually looked at the code in the ugliest of it's form.

tim_m said:
first, php 5 uses __construct not __constructor as per
http://us2.php.net/manual/en/language.oop5.decon.php

when you say x::manage_class(), you're intending to call a static method, but you need to define manage_class() as static
public static function manage_class() ...

if you call it statically, then there will be one instance of the class within the method when you call it. it dies once the method finishes running

tim_m, you are right about the __construct. My bad, I was typing really quickly and I made a mistake.

However I have to say that I disagree with you on your second point. You CAN call a public method from outside a class with out that method being static. I have slightly rewritten my previous class to show this:

PHP:
 <?php
// Tested with PHP 5.0.0
class test
{
  function __construct()
  {
    //init properties
  }
  public function manage_class()
  {
    $x= new test();
    echo "manage_class() running ok \n";
    unset($x);
    echo "manage_class() STILL running ok \n";
  }
}

test::manage_class();
/* - Output
manage_class() running ok
manage_class() STILL running ok
*/
?>

So again what is really going on? What I know of OOP tells me that a public method is only callable from an instance of the class and not directly from outside the class. Obviously this is not the case here. Someone shit some light on this. How many instances of the class are there? How is it possible for the manage_class() method to be called from outside the class? :confused:
 
petesmc said:
If you didn't return the value of $x (btw, you are missing a var $x), then after the manage)class() method runs, it will remove anything created, hence, 0 classes.

$x does NOT have to be a property of the class.
 
i think the issue here is that php 5 is not quite oop in the sense of java. maybe you have the flag set in your php.ini to allow php 4 oop compatibility in which there was no static keyword for methods
 
tim_m said:
i think the issue here is that php 5 is not quite oop in the sense of java. maybe you have the flag set in your php.ini to allow php 4 oop compatibility in which there was no static keyword for methods

php.ini file setting:
; Enable compatibility mode with Zend Engine 1 (PHP 4.x)
zend.ze1_compatibility_mode = Off

I don't think that is the issue.

Does anyone get an error when they run the above code in PHP 5?
 
(i don't actually run php 5, everything i say about it is from the limited amount i've read about it :eek:)
 
tim_m said:
(i don't actually run php 5, everything i say about it is from the limited amount i've read about it :eek:)

It was a good idea. For a moment I thought that my php.ini file was no being loaded. I did a manual load with php -c, same result - it works fine.
 
Back
Top