Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Anonymous class in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 111
    Comment on it

    Welcome to Findnerd. We have discussed the anonymous function. Today we are going to discuss the anonymous class. Here is the same concept. It does not have no name. It can extends the class and implement the interface to use these menthods and properties.Please have a look.

    <?php
    Class Student
    {
    protected $name = "deepak";
    private id;
    public marks = 343;
    protected function getMarks(){
    return $this->st_marks;
    }
    public function getData(){
    return new class($this->id) extends Student
    {
    private $st_id;
    public function __construct($id){
    $this->st_id = $id;
    }
    public function getRecords()
    {
    return 'name:' . $this->name . '<br />' . 'student id:' . $this->getmarks() . '<br />' . 'marks=' . $this->st_id; 
    }
    }
    }
    
    }
    echo (new Student(34))->getData()->getRecords();
    ?>
    

    In above example we created a class Student which have two functions named getData and getMarks. We created anonymous class in getData method and passed the private variable named id in it. Using this anonymous class we can use the properties and method of Student class and can make other function as well. We will call a function of anonymous class with object of Student class.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: