• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[PHP] Message Class

Joined
Sep 1, 2011
Messages
453
Reaction score
191
I still consider my self noob so, any input on how I can better myself is greatly appreciated! ;)

mysql.class.php
PHP:
<?php
    ///////////////////////////////////////////////////////Message Class (PHP) ////\\
    //
    //Copyright (C) 2012  Secured of RaGEZONE
    //
    /////////////////////////////////////////////////////////////////////////////////
    
    /////////////////////////////////////////////////////////////Documentation ////\\
    //7 Different Types of functions
    //Usage: $doMesage->(*Type*, *Message*, *var1*, *var2, ect....);
    //Types: 1:print, 2:printf, 3:vprintf, 4:sprintf, 5:print_r, 6:echo, 7:return
    //Ex Code: $doMsg->("5", "Random Echo Message");
    //Outcome: Random Echo Message
    //Echos a Message and Returns the Added Message can be used with
    //$var = "foo";
    //$doMsg->("5", $var");
    //Outcome: Foo
    //////////////////////////////////////////////////////////////////////////////////
    
    
    class Msg {
        private $type;
        private $msg;
        private $status;
        private $earlyF;

    	public function checkInvalidFormat($input) 
        {
            //Some Errors
			if($input <= 0)
            {
                $this->status=NULL;
            } else {
            
                $this->status=1;
            }
		}
        
        public function checkStatus($Var) 
        {
            if($Var==NULL)
            {
                $ei = debug_backtrace();
                $this->earlyF = "Msg Class:: Incorrect Format. Using function " . $ei[0]['function'] . " on line " . $ei[0]['line'] . " in file " . $ei[0]['file'] . ".";
                return false;
                
            } else if ($Var==1) {
            
                return true;
                
            }   else {
                $ei = debug_backtrace();
                $this->earlyF =  "Msg Class:: Status Error. Using function " . $ei[0]['function'] . " on line " . $ei[0]['line'] . " in file " . $ei[0]['file'] . ".";
                return false;
            }
        }
        
        public function buildMsg($type, $message) {
            if($type == "1")
            {
                print $message;
                
            } else if($type == "2") {
                
                printf($message);
                
            } else if($type == "3") {
            
                vprintf($message);      
                
            } else if($type == "4") {
            
                printf($message);        
                
            } else if($type == "5") {
            
                print_r($message);    
        
            } else if($type == "6") {
            
                echo $message;
            
            } else if($type == "7") {
            
                return($message);
            }
            
        }
        
        public function sMsg()
        {    
            //Grab all arguments slice first argument into two for type and msg and convert to string
            $arg  = func_get_args();
            $this->type = implode(array_slice($arg, 0, 1));
            $this->msg  = implode(array_slice($arg, 1)); 

            //check format
            $this->checkInvalidFormat($this->type);
            
            //check status
            if(!$this->checkStatus($this->status) == true)
            {
                $final=2;
                
            } else {
                
                $final=1;
            }
            if($final !=1) {
                
               $this->buildMsg("5", $this->earlyF); 
            
            } else {
            
               $this->buildMsg($this->type, $this->msg);
            }

        }
    }
?>

Example of how it can be ran.
PHP:
<?php
//include
include_once "inc/class/msg.class.php";

//test vars
$var1 = "1";
$var2 = "2";
$var3 = "3";
$var4 = "4";
$var5 = "5";

//test functions
function dostuff()
{
    $var6 = "stuff done!";
    return $var6;
}

function dostuff2()
{
    $var6 = "stuff done again!!";
    return $var6;
}

$msg = New Msg;
$msg->sMsg("6", $var1, $var2, $var3, $var4, $var5, dostuff(), dostuff2());

?>

Outcome:
Code:
12345stuff done!stuff done again!!
 
Not working on UnitedFlyf
Loyal Member
Joined
Apr 21, 2009
Messages
1,385
Reaction score
934
You should standardize your code formatting more, other than that it looks fine for the most part. However, why not just use the standard printf? I don't see much use for this class as the following would produce the same output.

PHP:
<?php
function test1()
{
    return "stuff done!";
}

function test2()
{
    return "stuff done again!!";
}

printf("%d%d%d%d%d%s%s", 1, 2, 3, 4, 5, dostuff(), dostuff2());
?>

Here's some useful documentation if you want to use the standard of php code formating.


Also, @Jayden, PHP's support for object oriented coding isn't even complete as far as I know. The standard of object oriented programing itself isn't even definite, why do you say this is using OOPHP improperly? It may be a bit scattered and unnecessary, but that does not make it incorrect usage of OOP.
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
I'm sorry, but I don't like it.
The ugly structure put aside, this class is completely and absolutely unnecessary.

Don't over-complicate. Wrapping PHP's print-esque functions in a class is a blatant example of over-complicating code, which a lot of novice programmers fall victim to.

Doing something cool < Doing something simply.

I'm not sure if I've told this story on RZ yet, but, early in Apple's lifetime, they had their programmers log how many lines of code they wrote for the day.
One of their programmers logged negative numbers on a pretty consistent basis.
Can you figure out why? Tip: It's not a bad thing.
 
Joined
Feb 18, 2012
Messages
779
Reaction score
247
You should standardize your code formatting more, other than that it looks fine for the most part. However, why not just use the standard printf? I don't see much use for this class as the following would produce the same output.

PHP:
<?php
function test1()
{
    return "stuff done!";
}

function test2()
{
    return "stuff done again!!";
}

printf("%d%d%d%d%d%s%s", 1, 2, 3, 4, 5, dostuff(), dostuff2());
?>

Here's some useful documentation if you want to use the standard of php code formating.


Also, @Jayden, PHP's support for object oriented coding isn't even complete as far as I know. The standard of object oriented programing itself isn't even definite, why do you say this is using OOPHP improperly? It may be a bit scattered and unnecessary, but that does not make it incorrect usage of OOP.

Well, we all have our habbits. I just wouldn't do it like so, as for you saying that OOP (isnt finished)? You're not entirley wrong but I wasn't talking about oop in general I was talking about his "Habbits".
 
Joined
Sep 1, 2011
Messages
453
Reaction score
191
I'm sorry, but I don't like it.
The ugly structure put aside, this class is completely and absolutely unnecessary.

Don't over-complicate. Wrapping PHP's print-esque functions in a class is a blatant example of over-complicating code, which a lot of novice programmers fall victim to.

Doing something cool < Doing something simply.

I'm not sure if I've told this story on RZ yet, but, early in Apple's lifetime, they had their programmers log how many lines of code they wrote for the day.
One of their programmers logged negative numbers on a pretty consistent basis.
Can you figure out why? Tip: It's not a bad thing.

Made for learning not for actual use tho its use able for people who integrate template systems into there websites
 
Not working on UnitedFlyf
Loyal Member
Joined
Apr 21, 2009
Messages
1,385
Reaction score
934
Well, we all have our habbits. I just wouldn't do it like so, as for you saying that OOP (isnt finished)? You're not entirley wrong but I wasn't talking about oop in general I was talking about his "Habbits".

OOP is generally recognized as object oriented programming. PHP is still is lacking quite a bit of standard object oriented programming features. PHP does not support virtual functions yet, which is a major portion of polymorphism.

Also, you said:
If your going to use oop - use it properly.

I don't see how that can be referring to habbits. Perhaps I should be a bit more clear. Let me ask you, what is proper OOP code if this is improper?
 
Joined
Feb 18, 2012
Messages
779
Reaction score
247
OOP is generally recognized as object oriented programming. PHP is still is lacking quite a bit of standard object oriented programming features. PHP does not support virtual functions yet, which is a major portion of polymorphism.

Also, you said:


I don't see how that can be referring to habbits. Perhaps I should be a bit more clear. Let me ask you, what is proper OOP code if this is improper?

Eh, TBH There is no "proper" way - Just like if you make an HTML template out of pure html no css and it is compatible with all browsers but it isnt proper. Do you understand now? :p:
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
OOP is generally recognized as object oriented programming. PHP is still is lacking quite a bit of standard object oriented programming features. PHP does not support virtual functions yet, which is a major portion of polymorphism.

Also, you said:


I don't see how that can be referring to habbits. Perhaps I should be a bit more clear. Let me ask you, what is proper OOP code if this is improper?

There are good and bad practices, albeit they are still subject to bias.
 
Not working on UnitedFlyf
Loyal Member
Joined
Apr 21, 2009
Messages
1,385
Reaction score
934
There are good and bad practices, albeit they are still subject to bias.

Anything that compiles and runs is "proper" code in my book. It might not be the most organized or efficient way, but it's still proper. There are good and bad practices, yes, but that's based on opinion(even if said opinion is logical). I just think "proper" is a bad word choice as it's pretty unclear especially when saying that they're using something non-definite like OOP improperly. The coding practices may be bad use of OOP in terms of organization and performance, but explain why rather than acting as though there is a set-in-stone correct method to using OOP efficiently.
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
Anything that compiles and runs is "proper" code in my book. It might not be the most organized or efficient way, but it's still proper. There are good and bad practices, yes, but that's based on opinion(even if said opinion is logical). I just think "proper" is a bad word choice as it's pretty unclear especially when saying that they're using something non-definite like OOP improperly. The coding practices may be bad use of OOP in terms of organization and performance, but explain why rather than acting as though there is a set-in-stone correct method to using OOP efficiently.

I will very much so agree that "proper" is a poor choice of words.

I think the most important thing to always keep in mind the premise - the goal or advantage, if you will - of the OOP paradigm.
That, although arguably a bias, would be maintainability.
The level of maintainability of an OOP application can be linked to numerous things, including how well your code is organized and structured, as well as using good practices and not bad, and so-on.
 
Back
Top