Results 1 to 4 of 4

Thread: declaring variable and function in PHP class

  1. #1
    Join Date
    May 2007
    Location
    Canada
    Posts
    85
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default declaring variable and function in PHP class

    I'm trying to get into writing OOP in PHP. Using PHP5. I know the basic structure of a class, variable and functions, but getting confused when looking at different tutorial.

    Inside a class, some are declaring function like:

    PHP Code:
    public function doSomething() 
    while others are writing

    PHP Code:
    public doSomething() 
    Some are even writing

    PHP Code:
    function doSomething() 
    Now, what is the difference in here? Who is right? I think the first two are more like how OOP should be (correct me if I'm wrong).

    And, what about declaring variables? I believe it should it be:

    PHP Code:
    public $myVar 
    Hope you can clarify my confusion here. Thanks

  2. #2
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Check out PHP.net's explanation:

    http://www.php.net/public
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    public function doSomething()
    the first one is syntactically the most correct, however PHP5 might possibly allow the others

  4. #4
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    if it is not declared PHP 5 declares it as public, but it is a good practice to always define it. Here is how the visibility works:
    PHP Code:
    class myClass {
    public 
    $public="public";
    private 
    $private="private";
    protected 
    $protected="protected";

    public function 
    print_vars() {
    echo 
    $this->public;
    echo 
    $this->private;
    echo 
    $this->protected;
    }
    }
    class 
    myClass2 extends myClass {
    public function 
    print_vars2() {
    echo 
    $this->public;
    echo 
    $this->private;
    echo 
    $this->protected;
    }
    }
    $test1=new myClass;
    $test2=new myClass2
    Here is how it would output:
    echo $test1->public; - "public"
    echo $test1->private; - fatal error
    echo $test1->protected; - fatal error
    echo $test2->public; - "public"
    echo $test2->private; - undefined
    echo $test2->protected; - fatal error
    $test1->print_vars(); - "public" "private" "protected"
    $test2->print_vars2(); - "public" undefined "protected"
    Last edited by Master_script_maker; 03-17-2008 at 08:08 PM.
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@ /\/\@|{
    There are 10 kinds of people in the world, those that understand binary and those that don't.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •