For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > Why an "abstract" access control modifier?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Why an "abstract" access control modifier?
Dag Magnuson

2006-10-17, 6:56 pm


Hi all, I was wondering if someone could give me an example of why and
how you would use an abstract modifier for a class?
.:[ ikciu ]:.

2006-10-17, 6:56 pm

Hmm Dag Magnuson <dagmagnuson@yahoo.com> wrote:
> Hi all, I was wondering if someone could give me an example of why and
> how you would use an abstract modifier for a class?


abstract class foo

It is not allowed to create an instance of a class that has been defined as
abstract. Any class that contains at least one abstract method must also be
abstract. Methods defined as abstract simply declare the method's signature
they cannot define the implementation.

When inheriting from an abstract class, all methods marked abstract in the
parent's class declaration must be defined by the child; additionally, these
methods must be defined with the same (or weaker) visibillity. For example,
if the abstract method is defined as protected, the function implementation
must be defined as either protected or public.



--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this => mysql_query();


Koncept

2006-10-20, 6:56 pm

In article <dk9Zg.4438$qv6.2356@trnddc06>, Dag Magnuson
<dagmagnuson@yahoo.com> wrote:

> Hi all, I was wondering if someone could give me an example of why and
> how you would use an abstract modifier for a class?


Sure. I am taking the following from a great tutorial on devshed. I
will give you the URL following the example.

First, create an abstract class

abstract class dataSaver {
protected $data;
protected function setData($data) {
$this->data = $data;
}
// abstract 'save()' method
abstract protected function save();
}

// Now that the abstract class is defined, you can subclass it

class fileDataSaver extends dataSaver {
private $file;
public function __construct($data, $file) {
// call parent method 'setData()'
parent::setData($data);
$this->file = $file;
}
// save data to file
public function save() {
if(!$fp=fopen($this->file.'.txt','a')){
throw new Exception('Error opening target file');
}
fwrite($fp,$this->data);
fclose($fp);
}
}

Here is the article by Alejandro Gervasio. I really like his tutorial
on devshed. You should give them a read.

(watch the wrap )
http://www.devshed.com/c/a/PHP/Abst...rking-with-PHP-
5/

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com