TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   What do you find more comfortable? (http://www.talkphp.com/general/2492-what-do-you-find-more-comfortable.html)

TlcAndres 03-19-2008 10:05 AM

What do you find more comfortable?
 
Well I use my own framework for coding (it's time for a complete rewrite) and some of my friends use as well and I'd like the opinion of some fellow coder and what you find more comfortable using (it's also for programmers who work on my script after I deliver it to client)

methods like this
PHP Code:

<?php

$usercheck 
member::login('chuck','secret');

?>

or

PHP Code:

<?php

$member 
= new member();
$usercheck $member->login('chuck','secret');

?>


Haris 03-19-2008 10:48 AM

I find the latter example more easier. :-)

dschreck 03-19-2008 03:27 PM

in my experience, using a static method like in your first example is a lot easier and faster to type.

Usually I use static methods for carrying around instances of objects I will want. For example:
PHP Code:

$db Core::DB(); 

Returns my current instance of the database object.

I also use them for a quick authentication check:

PHP Code:

Auth::checkUser($_SESSION['Vars']['go']['Here']); 

I just find that it takes less time to type one line, and why should I create a whole new instance of an object just to run one method ?

Gareth 03-19-2008 03:39 PM

This is noobish but what does the :: do?

Orc 03-19-2008 03:42 PM

Quote:

Originally Posted by Gareth (Post 12539)
This is noobish but what does the :: do?

It grabs a method/member from a class, such as a member would be a variable, and a method would be a function. Its just encapsulation differences. :: is a double resolution or so as double colon, in hebrew it gives you an error ( 'Paarayadim Neuktodrim' ) something rather, so many are confused at that. Anyway, its just to grab a certain method/member from a class, thats it.

PS a property is also a variable. thus either way
Update:
Gah, maybe I need to stay off the computer for 20 years.

Orc 03-19-2008 03:44 PM

By the way, I got the OP confused, -_-.

Update: Well, my new opinion -_- Is that you should do
If the class is in the php file
PHP Code:

<?php

$usercheck 
member::login('chuck','secret');

?>

otherwise you could always do the magic method:
PHP Code:

function __autoload($php)
{
  include 
'classes/'.$php.'.php';


This grabs every single class in classes/ directory, and includes them.

Something is wrong with me today -_-

xenon 03-19-2008 10:04 PM

Quote:

Originally Posted by Orc (Post 12540)
It grabs a method/member from a class, such as a member would be a variable, and a method would be a function. Its just encapsulation differences. :: is a double resolution or so as double colon, in hebrew it gives you an error ( 'Paarayadim Neuktodrim' ) something rather, so many are confused at that.

Question: what the hell are you talking about? *!*

Quote:

Originally Posted by Orc (Post 12540)
Anyway, its just to grab a certain method/member from a class, thats it.

No, it's not. Check this out:

Code:

class Test
{
        protected $x = 2;
       
        public static function first_method()
        {
                return $this->x;
        }
       
        public static function second_method()
        {
                return 'cant use $this in static methods';
        }
       
        private static function third_method()
        {
                return 'you cant call me na na na na na';
        }
}

Test::first_method(); // Fatal error: Using $this when not in object context
Test::second_method(); // the right call
Test::third_method(); // Fatal error: Call to private method Test::third_method() from context ''

So you see, you can't just 'grab' any method/property from a class.

Tlc: it's pretty simple. Do you need an instance of a class to authenticate a user, for example? Then don't use static methods. Static methods are made for defining behavior, not functionality.

Orc 03-19-2008 10:13 PM

Quote:

Originally Posted by xenon (Post 12561)
Question: what the hell are you talking about? *!*



No, it's not. Check this out:

Code:

class Test
{
        protected $x = 2;
       
        public static function first_method()
        {
                return $this->x;
        }
       
        public static function second_method()
        {
                return 'cant use $this in static methods';
        }
       
        private static function third_method()
        {
                return 'you cant call me na na na na na';
        }
}

Test::first_method(); // Fatal error: Using $this when not in object context
Test::second_method(); // the right call
Test::third_method(); // Fatal error: Call to private method Test::third_method() from context ''

So you see, you can't just 'grab' any method/property from a class.

Tlc: it's pretty simple. Do you need an instance of a class to authenticate a user, for example? Then don't use static methods. Static methods are made for defining behavior, not functionality.

Ahh yes the static, otherwise I don't get you. Visiblity, what about it? I understand public, private, protected.

freenity 03-20-2008 03:08 PM

I would suggest a bit different approach. What login should do? I suppose create a session/cookies, etc
So that's what I would prefer:

PHP Code:

$user = new Member('username','pass');
if (
$user->auth())    //checks if the password and username are correct
    
$user->login();   //sets the cookies, etc 

or maybe put the auth() part inside the login() and make it return true or false if the username/pass are correct or not.


All times are GMT. The time now is 09:29 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0