1/19
code
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
<?php
class Student {
public $name = "John";
}
$s = new Student();
echo $s->name;
?>John
<?php
class Greeting {
public function sayHello() {
echo "Hello!";
}
}
$g = new Greeting();
$g->sayHello();
?>
Hello!
<?php
class Student {
private $name = "John";
}
$s = new Student();
echo $s->name;
?>Fatal Error: Cannot access private property Student::$name
<?php
class Sample {
private function message() {
echo "Welcome";
}
}
$obj = new Sample();
$obj->message();
?>Fatal Error Call to private method Sample::message()
<?php
class Person {
protected $age = 20;
}
$p = new Person();
echo $p->age;
?>Fatal Error – Cannot access protected property
<?php
class Animal {
protected function sound() {
echo "Roar";
}
}
class Lion extends Animal {
public function makeSound() {
$this->sound();
}
}
$l = new Lion();
$l->makeSound();
?>Roar
<?php
class Animal {
protected function sound() {
echo "Meow";
}
}
$a = new Animal();
$a->sound();
?>Fatal Error Call to protected method Animal::sound()
<?php
class ParentClass {
private function greet() {
echo "Hello";
}
}
class Child extends ParentClass {
public function callGreeting() {
$this->greet();
}
}
$c = new Child();
$c->callGreeting();
?>Fatal error: Call to private method ParentClass::greet()
<?php
class Employee {
protected $salary = 30000;
}
class Manager extends Employee {
public function display() {
echo $this->salary;
}
}
$m = new Manager();
$m->display();
?>30000
<?php
class Book {
public $title = "PHP";
}
$b = new Book();
$b->title = "Java";
echo $b->title;
?>Java
<?php
class Student {
private $id = 101;
public function getID() {
return $this->id;
}
}
$s = new Student();
echo $s->getID();
?>101
<?php
class Vehicle {
protected $speed = 120;
public function getSpeed() {
return $this->speed;
}
}
$v = new Vehicle();
echo $v->getSpeed();
?>120
<?php
class Counter {
private $count = 0;
public function increment() {
$this->count++;
}
public function display() {
echo $this->count;
}
}
$c = new Counter();
$c->increment();
$c->increment();
$c->display();
?>2
<?php
class Calculator {
private function add($a, $b) {
return $a + $b;
}
public function compute() {
echo $this->add(5,3);
}
}
$c = new Calculator();
$c->compute();
?>8
<?php
class ParentClass {
public function message() {
echo "Parent";
}
}
class Child extends ParentClass {
public function message() {
echo "Child";
}
}
$obj = new Child();
$obj->message();
?>Child
<?php
class Shape {
protected function area() {
echo "Area";
}
}
class Square extends Shape {
public function calculate() {
$this->area();
}
}
$s = new Square();
$s->calculate();
?>Area
<?php
class ParentClass {
public $value = 50;
}
class Child extends ParentClass {
}
$obj = new Child();
echo $obj->value;
?>50
<?php
class ParentClass {
private $name = "Parent";
}
class Child extends ParentClass {
private $name = "Child";
public function display() {
echo $this->name;
}
}
$c = new Child();
$c->display();
?>Child
<?php
class User {
protected $username = "admin";
public function show() {
echo $this->username;
}
}
$u = new User();
$u->show();
?>admin
<?php
class Demo {
public $a = 10;
protected $b = 20;
private $c = 30;
public function show() {
echo $this->a;
echo $this->b;
echo $this->c;
}
}
$d = new Demo();
$d->show();
?>102030