Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Inheritance problem with OOP
Old 06-07-2010, 02:57 PM Inheritance problem with OOP
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
I'm getting to grips with Object Oriented PHP, but there's something I really don't understand about inheritance.

See this script:
PHP Code:
<?php

/**
 * Define MyClass
 */
class MyClass
{
    public 
$public 'Public';
    protected 
$protected 'Protected';
    private 
$private 'Private';
}

$obj = new MyClass;
echo 
$obj->public;

?>
I can access the public property fine, but when I try to echo 'protected' or 'private', I just see a blank screen!

I thought they would all be accessible from this object? I thought protected wouldn't be accessible from an object that wasn't of 'MyClass' (or any of its parents / children) and that private would only be accessible from an object of MyClass.

I'm guessing I have the wrong idea?

Also, I very rarely see any error messages with and OOP scripts! I have both XAMPP and MAMP installed on my Mac, but I mostly just see a blank screen when something goes wrong.

Do I need to change the error settings for my PHP installation or is this normal behaviour?
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
 
Register now for full access!
Old 06-07-2010, 03:07 PM Re: Inheritance problem with OOP
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
You cannot access private or protected member variables or methods from outside of a class. Protected variables can be accessed from a parent or child class, as well as the class itself. Private variables can only be accessed from the class itself. You should be seeing an error message like this:
Quote:
Fatal error: Cannot access private property MyClass::$private
Make sure error reporting is enabled in your php.ini

The code you posted doesn't really have anything to do with inheritance. Here is an example of inheritance:
PHP Code:
class MyClass
{
    public 
$a 'Public';
    protected 
$b 'Protected';
    private 
$c 'Private';
}

class 
MyChildClass extends MyClass
{
     function 
foo()
     {
          echo 
$this->b;
     }
}

$obj = new MyChildClass;
echo 
$obj->a;
$obj->foo(); 
a can be accessed from outside of either class. b can be accessed from within either class. c can only be accessed within MyClass.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 06-07-2010 at 03:22 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 06-07-2010, 03:40 PM Re: Inheritance problem with OOP
pealo86's Avatar
Super Spam Talker

Posts: 876
Name: Matt Pealing
Location: England, north west
Trades: 0
Ohhhh yeah I meant to say 'Visibility', haha, Im sure you can tell I'm new to this.

Thanks.
__________________

Please login or register to view this content. Registration is FREE
pealo86 is offline
Reply With Quote
View Public Profile Visit pealo86's homepage!
 
Reply     « Reply to Inheritance problem with OOP
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.37297 seconds with 12 queries