Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,698 questions

57,007 answers

573 users

How to use single-level inheritance in PHP

3 Answers

0 votes
class A {
    public function print_s($s) {
        echo $s . "\n"; 
    } 
    public function print_() {
        echo 'class A' . "\n"; 
    }
}
class B extends A {
    public function print_s($s) {
        echo $s . "\n"; 
    }
    /*public function print_() {
        echo "class B" . "\n"; 
    }*/
}


$a = new A();
$b = new B();
 
$a->print_s('class A - php');
$a->print_();       
 
$b->print_s('class B - java'); 
$b->print_();       


 
/*
run:
 
class A - php
class A
class B - java
class A
 
*/
 

 



answered Jul 8, 2018 by avibootz
edited Sep 16 by avibootz
0 votes
// Basic single‑level inheritance

class Animal {
    public function speak() {
        return "Some generic sound";
    }
}

class Dog extends Animal {
    public function bark() {
        return "Woof!";
    }
}

$dog = new Dog();
echo $dog->speak(); // inherited
echo "\n";
echo $dog->bark();  // child-specific


 
/*
run:
 
Some generic sound
Woof!
 
*/
 

 



answered Sep 16 by avibootz
0 votes
// Overriding a parent method

class Animal {
    public function speak() {
        return "Some generic sound";
    }
}

class Cat extends Animal {
    public function speak() {
        return "Meow";
    }
}

$animal = new Animal();
$cat = new Cat();

echo $animal->speak(); // Some generic sound
echo "\n";
echo $cat->speak();    // Meow


 
/*
run:
 
Some generic sound
Meow
 
*/
 

 



answered Sep 16 by avibootz

Related questions

2 answers 346 views
1 answer 327 views
1 answer 251 views
251 views asked Jul 8, 2018 by avibootz
2 answers 384 views
1 answer 241 views
241 views asked Oct 29, 2015 by avibootz
...