Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

abstract class property and method

Posted on 2014-01-16
7
Medium Priority
?
568 Views
Last Modified: 2014-01-17
abstract class shape{
private $_property1
protected method1(){}
}

any class that extends abstract class shape
does not require properties
and depending upon when I do private,protected,public method
I get error must be visibility or better


please explain.
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39787336
What's the question?  PHP Abstract classes are documented here:
http://php.net/manual/en/language.oop5.abstract.php

The abstract class definition forces the programmer to extend the class, rather than to instantiate the class.
0
 

Author Comment

by:rgb192
ID: 39787373
questions are

1)
if I set properties in the parent class, why dont the children require the same properties

2)
and setting methods in the child class
what is the
 must be visibility or better
error
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39787384
Show us some code examples that illustrate the issues, please.  Thanks, ~Ray
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 34

Accepted Solution

by:
Slick812 earned 2000 total points
ID: 39787457
You should maybe read the class explanations in the PHP manual or tutorial to get the meaning they have. A Class Abstraction that you show has a DIFFERENT purpose in PHP programming than a Regular Class (no abstraction). You can make a normal Class in PHP and then extend that to another Class you write. AND can ADD properties and methods to your new class, AND use properties and methods in the parent class. Very useful in programming. But do you know why You would write and use an Abstract Class instead of a normal Class? This is Important, the reason that abstract classes are in PHP.
 Mostly abstracts are used as a MINIMUN  definition, NON-WORKING template or "Class Guideline" only to show the absolute minimum Properties, methods, that ALL sub-classes MUST have to work in a class inheritance environment ,OR say in a way you may understand - any sub-class will have at LEAST the required methods (same number parameters) as the parent abstract class, so any sub-class can work if the minimum methods are used.
OR as an Abstract Class is like a partially written class (not complete). It is much like a code-template, with blanks (in methods) that HAVE to be completed and filled in for the sub class extensions.

You say -
any class that extends abstract class shape does not require properties.
This is probably Not True in this context, but NO Class is required to have any properties, so the abstract and sun class may not have properties, but why would you write a class and not have ANY properties? ? ? ?.
The sub class already can use any Properties in the absrt parent class, so no need to RE-DECLARE define, the same property as the parent.
But your question here makes no sense to me? ?

in the PHP maanual thier example does have a PUBLIC function -
class ConcreteClass1 extends AbstractClass
{
    protected function getValue() {
        return "ConcreteClass1";
    }

    public function prefixValue($prefix) { // THIS IS PUBLIC
        return "{$prefix}ConcreteClass1";
    }
}

You have to have the abstrat method -
protected method1(){}

and ADD to the code so that it does something for your NEW subclass,
in your sub-class, , but you also MUST define (write) a PUBLIC FUNCTION to USE this  method1()  and do other code to do what your CLASS is written to do. ALL of this Class writing is for a purpose, to get PHP to group together code and have the class do "Code Work" that you need in a web page, You use regular Class for one purpose, and Abstract Class for another purpose, and an Interface for another Class purpose, that chance what you use for what your Web Page output is suppose to do.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 39787829
I *think* I understand the problem but don't know the explanation.
PHP seems to complain about a *private* member in an *abstract* class which should not be a problem at all, maybe a warning if it is completely unused.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39788538
This worked correctly for me.  If you have an example that is failing, please post it so we can be looking at exactly the same thing you're looking at, thanks.

<?php // RAY_temp_foo.php
error_reporting(E_ALL);


abstract class Shape
{
    private $_property1;
    protected function method1(){}
}

class Square extends Shape
{
    public function method1()
    {
        echo 'Square';
    }
}

$sq = new square;
$sq->method1();

Open in new window

0
 

Author Closing Comment

by:rgb192
ID: 39789959
Absolute minimum requirements.  Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question