Jump to content

[SOLVED] Bad usage of 'class' keyword?


roopurt18

Recommended Posts

Would you call it bad practice to expose "constants" through a "namespace" using the class keyword?

 

<?php

class Module {
  var $CONST1 = 1,
       $CONST2 = 2,
       $CONST3 = 3;
}

// Elsewhere in the code
switch($val){
  case $Module::CONST1:
    break;
  case $Module::CONST2:
    break;
  case $Module::CONST3:
    break;
}
?>

 

I can think of a few reasons not to do it, but the usage just appears more object oriented.

Link to comment
Share on other sites

It doesn't do much though, you're still fetching an object property since php4 doesn't support static properties, nor class constants.

 

If you're not stuck with php4, I'd suggest using class constants instead (it is said they're faster than regular constants too - although I've never tested that).

 

class Module {

  const CONST1 = 1;

  const CONST2 = 2;

  const CONST3 = 3;

}

 

Module::CONST1

 

 

You really only have three options: static properties, class constants, or properties. Any one might be the right choice on different occasions.

Link to comment
Share on other sites

It doesn't do much though, you're still fetching an object property since php4 doesn't support static properties, nor class constants.

 

Exactly.  I figured I'd go ahead and try it and as soon as I started to try and access one elsewhere I realized it wasn't going to happen.

 

You can only access a class var through an instance in PHP4, which means I would have had to create an instance of the object to be accessed globally.  So I guess the answer to my question is it leads to bad programming practice, at least in PHP4.

 

I had other concerns, if it had even worked, about the "constants" not really being constants.  And as far as typing is concerned, there's really not much difference between the two:

 

$var = Module::CONST_VALUE;  // Using mechanisms in PHP5

$var = MODULE_CONST_VALUE; // Using define()

 

I just prefer the OOP aspect of the first method more, which I'm sure you agree with.

 

Ultimately it was just a quick idea I had, figured I'd post before trying it out, and it ultimately turned into a scenario where it would have been better to remain silent and thought a fool than post and remove all doubt.  :D

Link to comment
Share on other sites

I just prefer the OOP aspect of the first method more, which I'm sure you agree with.

 

I do, but it has practical advantages too, besides just being nice and OOPish. Apart from the assumed (I read it in a Matt Zandstra book) superior performance, it also prevents name clashes (indeed like namespaces).

 

Ultimately it was just a quick idea I had, figured I'd post before trying it out, and it ultimately turned into a scenario where it would have been better to remain silent and thought a fool than post and remove all doubt.  :D

 

:D

 

Even though php4 doesn't support class constants, you can live without them:

 

function getConstant1(){

return 'somevalue';

}

Module::getConstant1();

 

Overly simple, yes, but constants only allow scalar values anyway. I doubt it gets much faster either.. :P

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.