Jump to content

PHP Classes


maxudaskin

Recommended Posts

Did you read the article I linked you too? Its a 3 part series as well, just take a look at the main sites tutorials section for the rest of them.

 

I did. So the only reason to have a class is to allow for the variable to be used again without worries?

Link to comment
Share on other sites

Did you read the article I linked you too? Its a 3 part series as well, just take a look at the main sites tutorials section for the rest of them.

 

I did. So the only reason to have a class is to allow for the variable to be used again without worries?

 

I wouldn't say thats the only reason, but it is one of the benifits.

Link to comment
Share on other sites

What other benefits are there?

 

Read the articles, or do you really need one personally written here for you?

 

In this PHP.NET code, does the A::Foo not work because you are calling foo without... umm

 

-> allows for $this to be used while :: does not?

 

As for that, I have no idea what you are talking about.

Link to comment
Share on other sites

Sorry... didn't post the code...

 

<?php
class A
{
    function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class B
{
    function bar()
    {
        A::foo();
    }
}

$a = new A();
$a->foo();
A::foo();
$b = new B();
$b->bar();
B::bar();
?>

Link to comment
Share on other sites

This guy is obviously a wind up.  I could write a book on benefits of object oriented programming, and I'm new to the idea, since only dabbling since September 2007.

 

But it's late, and what's the point when this guy will find some way to shoot whatever I write, out the sky anyway. Hehe

Link to comment
Share on other sites

It's natural to be confused, as programming is.  But honestly, if you look at those articles, as provided by the moderator, they are pretty much the simplest explanation I have seen to OOP PHP.

 

I admit yes, it is confusing, I get confused with inheritance, abstraction and bits like that, but the basic concept of classes is very, very simple.  Just take a very simple example...

 

You have a mobile phone, I presume.  Imagine a class called MobilePhone.  This can be "instantiated" by writing

$myPhone = new MobilePhone()

.  You now have an object, assigned to $myPhone (a variable in PHP, I'm sure you'll know).

 

You could now perform methods, which belong in that class, such as

$myPhone->dial(123);

or

$myPhone->sendsms(...);

and so on.

 

Classes, I think, are just a way of organising things.  I apologise if any syntax I've provided here is incorrect.  This is a bit rushed.  But seriously, those articles cover it all.

Link to comment
Share on other sites

So lets say I have three functions in a class: login.

 

$login = new login;

 

When someone goes to the login page, it will check if logged in:

 

if($login->checkLoggedin)

checkLoggedin returns false

 

How could I call a function in a class?

 

if($login->checkLoggedin){
    $login->displayForm; // Call the function... somehow
}

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.