Why doesn't this abstract class output anything in Php? - php

Why does this abstract class not work and output nothing?
<?php
abstract class Con {
function __construct($name);
}
}
class Shop extends Con {
function __construct($name) {
$this->shopname = $name;
}
function write() {
echo $this->shopname;
}
function outputdate() {
echo ' ' . date('Y');
}
function __destruct() {
$this->outputdate();
}
}

You cannot define some class in other class body. Instead, you must use PHP OOP features to extend one class from another.
class Shop extends Con{
...code goes here....
}
$shop = new Shop('shopname');
$shop->write();

You can't instantiate an abstract class. Also, you can't create a class within another class.
http://php.net/manual/en/language.oop5.abstract.php
http://en.wikipedia.org/wiki/Abstract_type
Check out this link if you are looking to subclass.
http://php.net/manual/en/keyword.extends.php

Related

How to extend php class to a class which ovverride it's functions

Alright, I have a structure like this:
class Creature{
public function sayHi(){
echo "Hi";
}
}
class HumanType extends Creature(){
}
class Human extends HumanType{
}
class Human232 extends Human{
public function sayHi(){
echo "Hello, bro";
}
}
class Human457 extend Human{
}
$Human = new Human232($id);
echo $Human->sayHi(); //Hello, bro
$Human2 = new Human457($id);
echo $Human2->sayHi(); //Hi
//And then I have this still to be implemented
class HumanCategory576{
public function sayHi(){
echo "Hi from the category!";
}
}
I have of course many classes like:
Human457,
Human458,
Human459,
Human600,
Human601
And also:
HumanCategory576,
HumanCategory577,
HumanCategory578,
HumanCategory579,
HumanCategory580
What I want to do is implement HumanCategory576 in a way that sayHi() would print "Hi from the category!" only if the Human class which is (I suppose) inheriting it is not ovveriding the function, like Human457.
I hope I was clear enough.
How do I do? Thanks
I am assuming that it was your intention for class HumanCategory576 to extend another class.
You can use is_callable() to check if HumanCategory576 parent has the function by:
is_callable('parent::sayHi'){
parent::sayHi();
}else{
echo "Hi from the category!";
}
Please take a is_callable in PHP's documentation.
<?php
class TestClass extends TestClassParent {
/** #brief Object initialisation callback
#returns void */
public function __construct() {
# do initialisation
# ...
# if we have a parent
if(is_callable('parent::__construct')) {
# then bubble up
parent::__construct();
}
}
}
?>

how to use method of main class from the extended class and avoiding constructor loop

class a
{
function __construct()
{
$x = new b();
$x->myFunction();
}
}
class b extends a
{
public function myFunction()
{
echo 'Here is myFunction';
}
}
$a = new a();
now i want to know how to access function myFunction of class b from class a and avoid the infinity loop.
As stated in the comments, what you want to do is Abstract the parent class. Thus allowing you to call children classes from it's parent.
As the comment suggested too, this question/answer has a range of examples of how you could achieve your task as you see below.
abstract class a {
function __construct() {
$this->myFunction();
}
}
class b extends a {
function myFunction() {
echo 'im from class b, sweet.';
}
}
Example/Demo
Warning/Note
You can not try to instantiate the parent (abstract) class:
Classes defined as abstract may not be instantiated
Source
Also...
This, in no way is how you should be calling/doing what you're trying to do. If you could tell us what task you're trying to achieve by doing this, we can help you further. Otherwise you'll constantly be writing code that isn't intended to work, only doing so due to "work arounds".
You should use abstract class here.
abstract class a{ // define abstract class a
// parent class constructor
function __construct(){
// some code here
}
function test(){
$this->myFunction();
}
// define abstract method myFunction
abstract function myFunction();
}
class b extends a{
// child class constructor calls parent class constructor
function __construct(){
parent::__construct();
}
// this method will be called
function myFunction(){
echo 'Here is myFunction';
}
}
$b = new b; // create object of child class
$b->test();
you can use $this to access child methods
Class a{
function call_child_method(){
if(method_exists($this, 'myFunction')){
$this->myFunction();
}
}
}
Class b extends a{
function myFunction(){
echo 'Here is myFunction';
}
}
$test = new b();
$test->call_child_method();
you will get output "Here is myFunction".

How to declare Abstract Class?

I am trying to make abstract method clear to me , so I wrote this code and test it. But when I run it the following error appear :
Fatal error: Class Book contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Book::ClassName) in C:\AppServ\www\index2.php on line 5
so where is the problem and why ?
<?php
class Book
{
abstract public function ClassName();
}
class firstBook extends Book{
public function ClassName()
{
echo "Called form class firstBook";
}
}
class secondBook extends Book{
public function ClassName()
{
echo "Called from class secondBook";
}
}
$o = new firstBook();
$o2 = new secondBook();
$o->Classname();
$o2->Classname();
?>
Class must also use abstract keyword
<?php
abstract class Book
{
abstract public function ClassName();
}
class firstBook extends Book{
public function ClassName()
{
echo "Called form class firstBook";
}
}
class secondBook extends Book{
public function ClassName()
{
echo "Called from class secondBook";
}
}
You cannot declare abstract function in a concrete class. Update your Book class to be abstract.
abstract class Book
{
abstract public function ClassName();
}
You class has to be abstract.
Change
class Book
to
abstract class Book
Also, you are calling the functions wrongly.
Replace
$o->Classname;
$o2->Classname;
to
$o->ClassName();
$o2->ClassName();
Putting it all together
<?php
abstract class Book
{
abstract public function ClassName();
}
class firstBook extends Book{
public function ClassName()
{
echo "Called form class firstBook";
}
}
class secondBook extends Book{
public function ClassName()
{
echo "Called from class secondBook";
}
}
$o = new firstBook;
$o2 = new secondBook;
$o->ClassName();
$o2->ClassName();
?>
OUTPUT :
Called form class firstBookCalled from class secondBook

Get Class Name from Object inside Object

I have 3 classes..
class 1 :
<?php
include "two.php";
include "three.php";
class One{
public function __construct(){
$two = new Two($this);
$three = new Three($two);
}
}
$api = new One;
?>
class 2 :
<?php
class Two extends AOP {
public function __construct($obj){
//blablabla
}
}
?>
class 3 :
<?php
class Three extends AOP {
public function __construct($obj){
echo get_class($obj);
}
}
?>
But I want the result must output "One".
How to get class name from object inside object?
In your design you have to implement a getter in class two:
class 2 :
class Two
{
private $myObj;
public function __construct($obj)
{
$this->myObj = $obj;
}
public function getMyObj()
{
return $this->myObj;
}
}
then in class 3, you can retrieve class 1:
class Three
{
public function __construct($obj)
{
echo get_class($obj->getMyObj());
}
}
Use the keyword extends to inherit another class. Since PHP does not support multiple inheritances directly. You can get the class that you extend from with parent::$property; or parent::method();. So, you probably want your code to look more like.
// three.php
class Three extends AOP{
public function __construct($obj){
echo get_class($obj);
}
}
// two.php
class Two extends Three{
public function __construct($obj){
parent::__construct($obj); // Constructors do not return a value echo works
}
protected function whatever($string){
return $string;
}
}
// one.php
include 'three.php'; // must be included first for Two to extend from
include 'two.php'
class One extends Two{
public function __construct(){
// change this part
parent::__construct($this); // uses the parent Constructor
echo $this->whatever('. Is this what you mean?'); // call method without same name in this class - from parent
}
}
$api = new One;
I would not use your structure at all, but this should give you an idea of inheritance.

Force someone to make his sub classes implement an interfaces' methods

I'm wondering how to force sub classes to implement a given interface method.
Let's say I have the following classes :
interface Serializable
{
public function __toString();
}
abstract class Tag // Any HTML or XML tag or whatever like <div>, <p>, <chucknorris>, etc
{
protected $attributes = array();
public function __get($memberName)
{
return $this->attributes[$member];
}
public function __set($memberName, $value)
{
$this->attributes[$memberName] = $value;
}
public function __construct() { }
public function __destruct() { }
}
I would like to force any sub class of "Tag" to implement the "Serializable" interface. For example, if i a "Paragraph" class, it would look this way :
class Paragraph extends Tag implements View
{
public function __toString()
{
print '<p';
foreach($this->attributes as $attribute => $value)
print ' '.$attribute.'="'.$value.'"';
print '>';
// Displaying children if any (not handled in this code sample).
print '</p>';
}
}
How can i force a developer to make his "Paragraph" class implement the methods from the interface "Serializable"?
Thanks for taking the time to read.
Just have the abstract class implement the interface:
interface RequiredInterface
{
public function getName();
}
abstract class BaseClass implements RequiredInterface
{
}
class MyClass extends BaseClass
{
}
Running this code will result in the error:
Fatal error: Class MyClass contains 1 abstract method and must
therefore be declared abstract or implement the remaining methods
(RequiredInterface::getName)
This requires the developer to code the methods of RequiredInterface.
PHP code example:
class Foo {
public function sneeze() { echo 'achoooo'; }
}
abstract class Bar extends Foo {
public abstract function hiccup();
}
class Baz extends Bar {
public function hiccup() { echo 'hiccup!'; }
}
$baz = new Baz();
$baz->sneeze();
$baz->hiccup();
It is possible for an abstract class to extend Serializable, as abstract classes do not need to be base classes
This adds a __constructor to your class Paragraph which checks to see if Serializable is implemented.
class Paragraph extends Tag implements View
{
public function __construct(){
if(!class_implements('Serializable')){
throw new error; // set your error here..
}
}
public function __toString()
{
print '<p';
foreach($this->attributes as $attribute => $value)
print ' '.$attribute.'="'.$value.'"';
print '>';
// Displaying children if any (not handled in this code sample).
print '</p>';
}
}

Categories