Hongmu Notes
Home Language Notes If you want to use Class B within Class A in PHP, how can you do it?
Language Notes PHP

If you want to use Class B within Class A in PHP, how can you do it?

If you want to use Class B within Class A in PHP, how can you do it?

If you want to use one class within another PHP class, you can leverage PHP's class instantiation mechanism to create a new class instance and use it.

Suppose you have two classes, A and B, where class A needs to use class B; you can instantiate class B within class A. For example:

class A {
    public function doSomething() {
        $b = new B();
        $b->doSomethingElse();
    }
}

class B {
    public function doSomethingElse() {
        // 执行一些操作
    }
}

In this example, the `doSomething()` method in Class A creates a new instance of Class B and invokes its `doSomethingElse()` method.

When you need to use Class B within multiple methods of Class A, you can consider instantiating Class B as an attribute of Class A. For example:

class A {
    private $b;
    
    public function __construct() {
        $this->b = new B();
    }
    
    public function doSomething() {
        $this->b->doSomethingElse();
    }
    
    public function doSomethingElse() {
        $this->b->doAnotherThing();
    }
}

class B {
    public function doSomethingElse() {
        // 执行一些操作
    }
    
    public function doAnotherThing() {
        // 执行一些操作
    }
}

In this example, the constructor of Class A creates a new instance of Class B and stores it in the private property `$b` of Class A. Subsequently, other methods in Class A can use the `$b` property to invoke methods of Class B.

Hope this helps you resolve the issue.

 

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

(Adaptive mobile version) PBootCMS template for new energy equipment websites; Download source code for substation insulation cabinet and generator websites – 0355

(Adaptive mobile version) PBootCMS template for new energy equipment websites; Download source code for substation insulation cabinet and generator websites – 0355 Practical Collection pbootcms Template

This set includes an adaptive mobile-friendly website template for new energy equipment and substation insulation cabinet generators (PbootCMS). The design features a modern industrial style, ideal for showcasing new energy equipment, power infrastructure, and technical solutions. It enables new energy enterprises to present their products online and attract clients within the power industry. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 37
(PC + WAP) Atmospheric Scroll-Effect Website Template; Electrical and Power Equipment Website Source Code Download – 1071

(PC + WAP) Atmospheric Scroll-Effect Website Template; Electrical and Power Equipment Website Source Code Download – 1071 Practical Collection pbootcms Template

A professional scrolling banner and PbootCMS website template designed for electrical and power equipment, compatible with both PC and WAP devices. Its modern and sophisticated design is ideal for showcasing electrical and power equipment, industrial products, or corporate branding. This template helps power equipment companies showcase their brands online and attract industry-specific clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 52
Responsive Valve Manufacturing Website Template 0637

Responsive Valve Manufacturing Website Template 0637 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the valve manufacturing industry, featuring a professional industrial design style that effectively showcases valve products, manufacturing processes, technical specifications, and industrial applications. It helps valve manufacturing enterprises demonstrate their product capabilities online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 41
Commonly used regular filters for locomotive collection

Commonly used regular filters for locomotive collection Summary of pitfalls

To obtain standardized content, significant effort must be devoted to data collection – it's essential to carefully define your collection rules. My criteria for measuring standardized content: Each paragraph should be enclosed within <p> and </p> tags; there should be no redundant HTML tags or characters unrelated to the topic. Data extraction method: Use regular expression extraction, then combine the results into <p>[Parameter 1]</p>; note that some content may not have a clear beginning or end...
👁 356
(Adaptive mobile version) News blog PBootCMS website template – HTML5 responsive article-based information website source code download: 0356

(Adaptive mobile version) News blog PBootCMS website template – HTML5 responsive article-based information website source code download: 0356 Practical Collection pbootcms Template

An adaptive mobile-friendly news blog and article publishing PbootCMS responsive website template built with HTML5 technology. Its clean and user-friendly design makes it ideal for personal blogs or information websites hosting articles. It serves as a perfect foundational choice for content creators looking to build their own personal writing platform. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 31